【问题标题】:Scrapy bot and shell return different results with same xpath query. Why?Scrapy bot 和 shell 使用相同的 xpath 查询返回不同的结果。为什么?
【发布时间】:2015-11-10 18:47:45
【问题描述】:

当我在scrapy bot 和scrapy shell 中执行相同的xpath 查询时,我得到了不同的结果。

注意:我只是想学习 scrapy 并修改一些教程代码。请跟我慢慢走。

查询:

xpath('//div/div/div/ul/li/a/@href')

机器人:

import scrapy

from tutorial.items import DmozItem

class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["lib-web.org"]
    start_urls = [
        "http://www.lib-web.org/united-states/public-libraries"
    ]

    def parse(self, response):
        for href in response.xpath('//div/div/div/ul/li/a/@href'):
            url = response.urljoin(href.extract())
            yield scrapy.Request(url, callback=self.parse_dir_contents)


    def parse_dir_contents(self, response):
        for sel in response.xpath('//ul/li'):
            item = DmozItem()
            item['title'] = sel.xpath('a/text()').extract()
            item['link'] = sel.xpath('a/@href').extract()
            item['desc'] = sel.xpath('p/text()').extract()
            yield item

DmozItem:

import scrapy

class DmozItem(scrapy.Item):
    title = scrapy.Field()
    link = scrapy.Field()
    desc = scrapy.Field()

我想要的只是指向州公共图书馆页面的链接(见网页)。

这是 shell 显示的内容(这正是我想要的):

Admin$ scrapy shell http://www.lib-web.org/united-states/public-libraries
...snip...
In [1]: response.selector.xpath('//div/div/div/ul/li/a/@href')
Out[1]: 
[<Selector xpath='//div/div/div/ul/li/a/@href' data=u'/united-states/public-libraries/alabama/'>,
 <Selector xpath='//div/div/div/ul/li/a/@href' data=u'/united-states/public-libraries/alaska/'>,
...snip. for brevity...
 <Selector xpath='//div/div/div/ul/li/a/@href' data=u'/united-states/public-libraries/wisconsi'>,
 <Selector xpath='//div/div/div/ul/li/a/@href' data=u'/united-states/public-libraries/wyoming/'>]

当蜘蛛运行相同的查询时,我得到了我不想要的其他 href 选择。

几个例子:

2015-11-10 13:27:52 [scrapy] DEBUG: Scraped from <200 http://www.lib-web.org/united-states/public-libraries/alabama/>
{'desc': [], 'link': [u'http://www.dirbuzz.com'], 'title': [u'DirBuzz.com']}
2015-11-10 13:27:52 [scrapy] DEBUG: Scraped from <200 http://www.lib-web.org/united-states/public-libraries/alabama/>
{'desc': [], 'link': [u'http://www.dirville.com'], 'title': [u'DirVille']}
2015-11-10 13:27:52 [scrapy] DEBUG: Scraped from <200 http://www.lib-web.org/united-states/public-libraries/alabama/>
{'desc': [], 'link': [u'http://www.duddoo.com'], 'title': [u'Duddoo.net']}

据我所知,机器人返回的许多元素/链接不适合 xpath 选择器。这是怎么回事?有人可以解释我做错了什么吗?

非常感谢!

【问题讨论】:

    标签: xpath scrapy scrapy-spider scrapy-shell


    【解决方案1】:

    查看您的parse 函数。此行response.xpath('//div/div/div/ul/li/a/@href') 将为您提供所需状态库的所有链接的列表。现在您正在遍历所有抓取的链接并使用此行yield scrapy.Request(url, callback=self.parse_dir_contents) 跟踪链接。然后你的机器人正在回调函数parse_dir_contents。在此函数中,您的机器人将选择 xpath //ul/li 中存在的所有元素。因此,您作为输出看到的链接实际上存在于后续链接的页面中,而不是 start_url's 页面中。这就是为什么 shell 输出和蜘蛛输出之间存在差异的原因。 shell 输出仅显示来自您传递给它的 url 的链接。您可以通过访问 url http://www.lib-web.org/united-states/public-libraries/alabama/ 来交叉检查您的结果,并检查它是否包含此 url http://www.dirbuzz.com

    【讨论】:

    • 是的。就是这样!我现在觉得很笨。菜鸟错误。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 2020-11-06
    • 2017-04-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2017-05-08
    相关资源
    最近更新 更多