我想通過進入頁面進入頁面來刮掉agent_name的聯繫人詳細信息。有時,這個腳本返回給我一個條目,有時候不同的條目無法找出原因。我的刮刀出了什麼問題?
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import Selector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from urlparse import urljoin
class CompItem(scrapy.Item):
title = scrapy.Field()
link = scrapy.Field()
data = scrapy.Field()
class criticspider(CrawlSpider):
name = "comp"
allowed_domains = ["iproperty.com.my"]
start_urls = ["http://www.iproperty.com.my/property/searchresult.aspx?t=S&gpt=AR&st=&ct=&k=&pt=&mp=&xp=&mbr=&xbr=&mbu=&xbu=&lo=&wp=&wv=&wa=&ht=&au=&sby=&ns=1"]
def parse(self, response):
sites = response.xpath('.//*[@id="frmSaveListing"]/ul')
items = []
for site in sites:
item = CompItem()
item['title'] = site.xpath('.//li[2]/div[3]/div[1]/div[2]/p[1]/a/text()').extract()[0]
item['link'] = site.xpath('.//li[2]/div[3]/div[1]/div[2]/p[1]/a/@href').extract()[0]
if item['link']:
if 'http://' not in item['link']:
item['link'] = urljoin(response.url, item['link'])
yield scrapy.Request(item['link'],
meta={'item': item},
callback=self.anchor_page)
items.append(item)
def anchor_page(self, response):
old_item = response.request.meta['item']
old_item['data'] = response.xpath('.//*[@id="main-content3"]/div[1]/div/table/tbody/tr/td[1]/table/tbody/tr[3]/td/text()').extract()
yield old_item
當您的代碼執行並且不起作用時,您看過網頁的變化嗎? – 2015-04-04 14:35:26
我檢查了網頁,它隨新的列表更改,但它應該拉出匹配xml路徑的數據? – nik 2015-04-04 14:36:40