如何获取以下html的'img src‘的xpath
<a class=product-tile">
<img src="image-file-here">
</a>发布于 2020-08-16 11:59:37
听起来您想要提取src属性的值,如果是这样的话,这应该可以解决问题:
response.xpath('//a[@class="product-tile"]/img/@src').get()发布于 2020-08-16 16:43:52
您可以使用CSS选择器,它更容易转换为XPATH底层。
尝尝这个
response.css('.product-tile ::attr(src)').get()发布于 2020-08-16 17:30:32
如果你正在寻找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()https://stackoverflow.com/questions/63432451
复制相似问题