【问题标题】:Empty List From Scrapy When Using Xpath to Extract Values使用 Xpath 提取值时 Scrapy 中的空列表
【发布时间】:2018-02-12 00:49:19
【问题描述】:

真的需要这个社区的帮助。

我的问题是,当我在 python 中使用代码时

response.xpath("//div[contains(@class,'check-prices-widget-not-sponsored')]/a/div[contains(@class,'check-prices-widget-not-sponsored-link')]").extract()

在scrapy shell中提取供应商名称,输出为空。我真的不知道为什么会这样,在我看来问题可能是网站信息正在动态更新?

这个网页抓取的网址是:https://cruiseline.com/cruise/7-night-bahamas-florida-new-york-roundtrip-32860,我需要的是每个供应商的供应商名称和价格。除了附图是“检查”的截图。 enter image description here

但是,类似的代码可以提取以下页面网址中的价格('https://cruiseline.com/destination/caribbean/cruise/best?sort=rank,ship_status&&direction=desc&page=1&per_page=10&sailing_counts=0')

Prices = response.xpath(
        "//div[contains(@class,'featured-cruise-price-inner-price')]/span/descendant::text()").extract()

非常感谢您的帮助!

【问题讨论】:

    标签: python xpath web-scraping scrapy


    【解决方案1】:

    我在scrapy shell中尝试了这个网址:https://cruiseline.com/cruise/7-night-bahamas-florida-new-york-roundtrip-32860,但我也一无所获

    response.xpath("//div[contains(@class,'check-prices-widget-not-sponsored')]/a/div[contains(@class,'check-prices-widget-not-sponsored-link')]").extract()

    然后我用 view(response) 命令来判断蜘蛛看到了什么,发现这个网站是动态的,这意味着如果你想在那个网站上抓取信息,你需要执行显示信息的js代码。

    截图如下:

    如您所见,您需要的信息并未显示。但是,这个https://cruiseline.com/destination/caribbean/cruise/best?sort=rank,ship_status&&direction=desc&page=1&per_page=10&sailing_counts=0 是静态的,所以这就是为什么你可以抓取你需要的东西。

    我有两种方法让你抓取动态网站(当然还有更多):

    1.Splash(Official Doc): 在您的 Spider 中,使用 SplashRequest 而不是 scrapy.Request 生成您的 url。

    2.Selenium + PhantomJS(Official Doc)

    祝你刮痧好运! :)

    【讨论】:

    • 非常感谢您的回答和解决方案!我现在正在尝试使用 Selenium 进行报废。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多