【发布时间】:2018-01-08 05:13:00
【问题描述】:
我正在 python 中工作,在 Scrapy 库的帮助下制作爬虫。当我使用选择器 response.xpath 和 response.css 获取数据时,它会给出不同的结果。就像当我使用 xpath 时它不显示结果,如果我替换 xpath
用css然后它显示结果。请帮助我理解这个概念。
xpath 查询
img = response.xpath('//div[@class="product-images"]//img/@src').extract()
css查询
img = response.css('div.product-images img::attr(src)').extract()
谢谢。
【问题讨论】:
-
div.product-images 元素有多个类吗? HTML 是什么样的?
-
是的,它不止一个类。
<div class="product-images relative mb-half has-hover woocommerce-product-gallery woocommerce-product-gallery--with-images woocommerce-product-gallery--columns-4 images">
标签: python xpath css-selectors scrapy