【问题标题】:how to parse multiple pages with scrapy如何用scrapy解析多个页面
【发布时间】:2012-01-20 09:06:48
【问题描述】:

我不断收到错误:

的语法无效
1.add_xpath('tagLine', '//p[@class="tagline"]/text()')

我似乎无法弄清楚为什么它会给我这个错误,因为据我所知,它与所有其他 1.add_xpath() 方法的语法相同。我的另一个问题是如何请求其他页面。基本上我正在浏览一个大页面并让它浏览页面上的每个链接,然后一旦完成页面,我希望它转到下一个大页面的下一个(按钮),但我不知道怎么做。

   def parse(self, response):
    hxs = HtmlXPathSelector(response)
    for url in hxs.select('//a[@class="title"]/@href').extract():
        yield Request(url, callback=self.description_page)
    for url_2 in hxs.select('//a[@class="POINTER"]/@href').extract():
        yield Request(url_2, callback=self.description_page)

def description_page(self, response):
    l = XPathItemLoader(item=TvspiderItem(), response=response)
    l.add_xpath('title', '//div[@class="m show_head"]/h1/text()')
    1.add_xpath('tagLine', '//p[@class="tagline"]/text()')
    1.add_xpath('description', '//div[@class="description"]/span')
    1.add_xpath('rating', '//div[@class="score"]/text()')
    1.add_xpath('imageSrc', '//div[@class="image_bg"]/img/@src')
    return l.load_item()

对此的任何帮助将不胜感激。当谈到 python 和 scrapy 时,我仍然是一个菜鸟。

【问题讨论】:

    标签: python debugging function web-scraping scrapy


    【解决方案1】:
    def description_page(self, response):
        l = XPathItemLoader(item=TvspiderItem(), response=response)
        l.add_xpath('title', '//div[@class="m show_head"]/h1/text()')
        1.add_xpath('tagLine', '//p[@class="tagline"]/text()')
        1.add_xpath('description', '//div[@class="description"]/span')
        1.add_xpath('rating', '//div[@class="score"]/text()')
        1.add_xpath('imageSrc', '//div[@class="image_bg"]/img/@src')
        return l.load_item()
    

    你有数字 1 而不是变量名 l

    【讨论】:

    • 你是男人中的神。非常感谢你。你是怎么抓到的?这很疯狂。 :) 你对为什么它只解析一页而不是每一页有什么想法吗?
    • 我想我明白了,因为我使用带有不同字体集的 linux。在我的字体中,1 和 l 之间的差异很大。 :)
    • 太棒了!我花了大约 20 分钟的时间查看代码的平静,试图找出不同之处。我想这对我来说是一个教训,不要使用这种简单易错的变量名。但再次感谢你。 :)
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多