我正在学习scrapy,现在正在玩它的 shell 。作为一个简单的练习,我想从此站点提取可见的房间图像:
https://www.gumtree.com/flats-houses/london
发射 shell 后:

scrapy shell "https://www.gumtree.com/flats-houses/london"

我正在使用下面的简单命令来完成这项工作:
response.xpath("//div[@class='listing-side']/div[@class='listing-thumbnail']/img/@src").extract()

但是,它将返回30个元素列表,其中25个值为空字符串。首先,我认为我的xpath一定有问题,所以我用 Chrome 工具测试了它,我必须说它像一个魅力一样返回了完整的图像URL列表。一切都如预期。但是,为什么scrapy不这样做呢?

编辑:

哦,很抱歉,实际上,在这个站点的情况下,启动 shell 程序必须发出以下命令:
shell -s USER_AGENT="Mozila/5.0" "https://www.gumtree.com/flats-houses/london"

换句话说,必须指定用户代理。

最佳答案

尝试通过以下方式进行操作:

listings = response.xpath("//div[@class='listing-thumbnail']")
images = [listing.xpath('.//img/@src').extract()[1] for listing in listings]

“图像”是包含所有列表图像的列表。

关于python - 爬取和 Chrome 工具返回的值不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46390265/

10-14 18:20
查看更多