如何获得嵌套选择器



如何获取以下html 的"img src"的xpath

<a class=product-tile">
<img src="image-file-here">
</a>

听起来你想提取src属性的值,如果是这样的话,这应该可以解决问题:

response.xpath('//a[@class="product-tile"]/img/@src').get()

选择器上的报废文档

您可以使用CSS选择器,它更容易并转换为XPATH引擎盖下。

试试这个

response.css('.product-tile ::attr(src)').get()

如果你正在寻找XPTH,这里是你可以用来获得图像源的第一个查找使用extract_first((

response.xpath('//img/@src').extract_first()

如果你有复杂的html,你可以使用更具体的xpath

response.xpath('//a[@class="product-tile"]/@src').extract_first()

如果要提取多个图像src链接,请使用extract((

response.xpath('//a[@class="product-tile"]/@src').extract()

像这个标签里面可能有不止一个src链接

<a class=product-tile">
<img src="image-file-here">
<a>
<img src="image-file-here">
</a>
<img src="image-file-here">
<img src="image-file-here">
</a>

所以在@src 之前使用//

response.xpath('//a[@class="product-tile"]//@src').extract()

最新更新