Python皮屑xpath:内部错误:(1136,u“列计数与第1行的值计数不匹配”)

2024-03-29 00:08:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码。什么时候我爬其他网址,这是没有问题的,但当我爬这个网址.it问我列不匹配。我不知道为什么计数长度是字符长度,而不是dict长度?你知道吗

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   

Tags: divlentitleextractliitemxpathlist
1条回答
网友
1楼 · 发布于 2024-03-29 00:08:27

我无法用代码重现你的错误信息。(scrapy 1.3.2Python 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开头的点。你知道吗

相关问题 更多 >