有我的代码。当我抓取其他网址时,这没问题,但是当我抓住这个网址时,请问我列不匹配。我不知道为什么计数长度是字符长度,而不是字典长度?
class JikespiderSpider(scrapy.Spider):
name = "jikespider"
allowed_domains = ["fromgeek.com"]
start_urls = ['http://www.fromgeek.com/topic/']
def parse(self, response):
sel = Selector(response)
jike_list = sel.xpath('//ul[@id="masonry0"]')
ll = len(sel.xpath('//ul[@id="masonry0"]/li'))
for jike in range(ll):
item = JikeItem()
try:
item['jike_title'] = jike_list.xpath('//li/div/div[@class="n-pic fl"]/a/@title').extract()[jike].strip()
item['jike_uptime'] = jike_list.xpath('//li/div/div[@class="n-keytime "]/div[@class="time fr"]/text()').extract()[jike].strip()
item['jike_tag'] = jike_list.xpath('//li/div/div[@class="n-keytime "]/div[@class="key fl"]').xpath('string(.)').extract()[jike].strip()
print len(item['jike_title'])
print len(item['jike_uptime'])
print len(item['jike_tag'])
print '--------------------------'
yield item
except Exception,e:
print e
答案 0 :(得分:0)
我无法使用代码重现您的错误消息。 (scrapy 1.3.2
,Python 2.7.11
)。
我想知道你为什么不循环selector list
但是建立一个计数器来访问这些元素。使用嵌套的XPath查询会更容易。
class JikespiderSpider(scrapy.Spider):
name = "jikespider"
allowed_domains = ["fromgeek.com"]
start_urls = ['http://www.fromgeek.com/topic/']
def parse(self, response):
sel_jike_list = response.xpath('//ul[@id="masonry0"]/li')
for sel_jike in sel_jike_list:
item = JikeItem()
item['jike_title'] = sel_jike.xpath('.//div[@class="n-pic fl"]/a/@title').extract_first()
# ... other fields
yield item
请注意嵌套XPath开头的点。