代码之家  ›  专栏  ›  技术社区  ›  Mattia Surricchio

Scrapy-Xpath可以在shell中工作,但不能在代码中工作

  •  1
  • Mattia Surricchio  · 技术社区  · 7 年前

    我正在尝试爬网一个网站(我得到了他们的授权),我的代码在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()
    

    我知道有一些问题 普罗多蒂 函数ecc。。。,但这不是重点。我想理解为什么xpath选择器与scrapy shell一起工作(我得到了所需的链接),但是当我在我的spider中运行它时,我总是得到一个空列表。

    如果可以的话,当我在spider中使用CSS选择器时,它可以很好地工作并找到元素,但我希望使用xpath(我在未来的应用程序开发中需要它)。

    感谢您的帮助:)

    编辑 : 我试图打印第一个响应的正文(从start\u URL),结果是正确的,我得到了我想要的页面。当我在代码中使用选择器(甚至是建议使用的选择器)时,它们在shell中都能正常工作,但我在代码中什么也得不到!

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

    已解决 : 路径正确。我写错了start\u URL!

    2 回复  |  直到 7 年前
        1
  •  1
  •   Granitosaurus    7 年前

    除了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 命令非常适合调试此类问题。

        2
  •  1
  •   Desperado    7 年前
        //nav[@id="mmenu"]//ul/li[contains(@class,"level0")]/a[contains(@class,"level-top")]/@href 
    

    使用此xpath,在创建xpath之前还要考虑页面的“查看源代码”