【问题标题】:Scrapy - Xpath works in shell but not in codeScrapy - Xpath 在 shell 中工作,但在代码中不工作
【发布时间】:2018-10-05 06:34:58
【问题描述】:

我正在尝试抓取一个网站(我得到了他们的授权),我的代码在 scrapy shell 中返回了我想要的内容,但我的蜘蛛没有得到任何东西。

我还检查了所有与此类似的上一个问题,但没有任何成功,例如,该网站没有在主页中使用 javascript 来加载我需要的元素。

import scrapy


class MySpider(scrapy.Spider):
    name = 'MySpider'

    start_urls = [ #WRONG URL, SHOULD BE https://shop.app4health.it/ PROBLEM SOLVED!
        'https://www.app4health.it/',
    ]

    def parse(self, response):
        self.logger.info('A response from %s just arrived!', response.url)
        print ('PRE RISULTATI')

        results =  response.selector.xpath('//*[@id="nav"]/ol/li[*]/a/@href').extract()
        # results = response.css('li a>href').extract()


        # This works on scrapy shell, not in code
        #risultati =  response.xpath('//*[@id="nav"]/ol/li[1]/a').extract()
        print (risultati)




        #for pagineitems in risultati:
               # next_page = pagineitems 
        print ('NEXT PAGE')
        #Ignores the request cause already done. Insert dont filter
        yield scrapy.Request(url=risultati, callback=self.prodotti,dont_filter = True)

    def prodotti(self, response):
        self.logger.info('A REEEESPONSEEEEEE from %s just arrived!', response.url)
        return 1

我要抓取的网站是https://shop.app4health.it/

我使用的 xpath 命令是这个:

response.selector.xpath('//*[@id="nav"]/ol/li[*]/a/@href').extract()

我知道 prodotti 函数 ecc... 存在一些问题,但这不是重点。我想了解为什么 xpath 选择器与 scrapy shell 一起工作(我得到了我需要的链接),但是当我在我的蜘蛛中运行它时,我总是得到一个空列表。

如果它有帮助,当我在我的蜘蛛中使用 CSS 选择器时,它可以正常工作并找到元素,但我想使用 xpath(我在未来的应用程序开发中需要它)。

感谢您的帮助:)

编辑: 我试图打印第一个响应的正文(来自 start_urls ),这是正确的,我得到了我想要的页面。当我在我的代码中使用选择器(甚至是建议的选择器)时,它们在 shell 中都可以正常工作,但我的代码中什么也没有!

编辑 2 我在使用 Scrapy 和网络爬虫方面变得更有经验,并且我意识到有时,您在浏览器中获得的 HTML 页面可能与您通过 Scrapy 请求获得的页面不同!以我的经验,与您在浏览器中看到的相比,某些网站会以不同的 HTML 响应!这就是为什么有时如果您使用从浏览器获取的“正确”xpath/css 查询,如果在您的 Scrapy 代码中使用它可能不会返回任何内容。 始终检查您的回复内容是否符合您的预期!

已解决: 路径正确。我写错了 start_urls!

【问题讨论】:

  • 调试101。scrapy能找到//*[@id="nav"]吗?如果是,则使表达式逐渐变得更复杂,以找到它中断的点。
  • 你能翻译你的 cmets 和变量名吗? // Potresti tradurre i tuoi commenti e i nomi delle variabili?
  • @Tomalak 即使使用该路径,它也会返回 null !但是,如果我尝试在 scrapy shell 中进行调试,它适用于该路径。
  • scrapy 看到的源代码是什么?
  • 解决了!我写错了 start_urls!

标签: python xpath web-scraping web-crawler scrapy-spider


【解决方案1】:

除了 Desperado 的回答之外,您还可以使用更简单但对于您的用例来说绰绰有余的 css 选择器:

$ scrapy shell "https://shop.app4health.it/"
In [1]: response.css('.level0 .level-top::attr(href)').extract()
Out[1]: 
['https://shop.app4health.it/sonno',
 'https://shop.app4health.it/monitoraggio-e-diagnostica',
 'https://shop.app4health.it/terapia',
 'https://shop.app4health.it/integratori-alimentari',
 'https://shop.app4health.it/fitness',
 'https://shop.app4health.it/benessere',
 'https://shop.app4health.it/ausili',
 'https://shop.app4health.it/prodotti-in-offerta',
 'https://shop.app4health.it/kit-regalo']

scrapy shell 命令非常适合调试此类问题。

【讨论】:

  • 非常感谢!你建议我使用 CSS 选择器而不是 xpath 吗?你能给我推荐一个很好的指南来学习如何正确使用它们吗?我还是一个网络爬虫的新手。
  • 这个 CSS 选择器在 shell 中运行良好,但在我的代码中却不行。我认为我的代码中有一些愚蠢的地方做错了,但我不知道是什么。我尝试打印初始响应正文,它工作正常,但是当我使用选择器时,我总是一无所获。
  • 好吧,您的代码正在爬取app4health.it 而不是shop.app4health.it。也许这就是问题? :)
【解决方案2】:
    //nav[@id="mmenu"]//ul/li[contains(@class,"level0")]/a[contains(@class,"level-top")]/@href 

使用这个xpath,在创建xpath之前还要考虑页面的'view-source'

【讨论】:

  • 你能解释一下我哪里错了吗?我进入页面并从浏览器中复制了 xpath。为什么该路径在 scrapy shell 中有效,但在我的代码中无效?非常感谢:)
  • 我尝试了这条路径,但我的选择器仍然为 null。您的路径在scrapy shell(如我的)中运行良好,但它们都不适用于我的代码。我还是不明白为什么。一般xpath可能有问题吗?非常感谢:)
  • 你可以试试 response.xpath('//nav[@id="mmenu"]//ul/li[contains(@class,"level0")]/a[contains(@class ,"level-top")]/@href').extract()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多