python - 使用xpath获取图像

标签 python xpath web-scraping scrapy web-crawler

我一直在尝试从这个网站https://www.fineandcountry.com/sa/property-for-sale/cape-town-clifton/property/929703抓取信息我在获取该属性的所有图像时遇到问题:它们位于属性样式内,这给我带来了一些困难。我一直在尝试做的事情:

images = response.xpath("//div[@class='search-results-gallery-property']
/a[@class='rotator_thumbs']/@style").extract()

但到目前为止,这是空的。

它是这样的:

<div class="search-results-gallery-property">
  <a style="background-image: 
url(https://static.propertylogic.net/property/8/200673/IMG_200673_3_small.jpg);" class="rotator_thumbs">
  </a></div>

关于我做错了什么/如何从属性样式中提取有什么建议吗?谢谢!

最佳答案

您尝试使用的类名似乎是动态生成的。这是它们在页面源代码中的样子:

<a href="https://static.propertylogic.net/properties/8/972/1900/929703/IMG_yPAH7LUOOusZs642oS1TjGTl9WIXaOUKWWMsXiSnL0luoixXTAyNV32n9kiM_hd.jpeg" class="fancybox-thumbs col-md-4 col-sm-6 col-xs-12" data-fancybox-group="property-images" style="margin-bottom: 10px; padding-right: 5px; padding-left: 5px;">
    <div class="col-md-12" style="background: url('https://static.propertylogic.net/properties/8/972/1900/929703/IMG_yPAH7LUOOusZs642oS1TjGTl9WIXaOUKWWMsXiSnL0luoixXTAyNV32n9kiM_small.jpeg'); height: 160px; padding-right: 0px; padding-left: 0px; background-size: cover; background-position: center;"> </div>
</a>

您可以使用两者中的任何一个来获取原始图像链接:

for item in response.css('a[data-fancybox-group="property-images"] > [style^="background"]::attr(style)').getall():
    yield {"image_link":item}

for item in response.xpath('//a[@data-fancybox-group="property-images"]/*[starts-with(@style,"background")]/@style').getall():
    yield {"image_link":item}

关于python - 使用xpath获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466893/

相关文章:

python - 上线时如何将数据库与 Django 网络应用程序分开?

Python -- matplotlib 椭圆曲线

python - 如何在PyAudio中获取 channel 数据

xml - R:如何获取站点时间的父属性和节点值?

python-3.x - 在后台使用 selenium chrome 浏览器(不是 headless 模式)?

python - 为什么 numpy.matrix 的 numpy.copy 的行为与原始矩阵不同?与该副本转置的乘法不起作用

java - 从命名空间 xml 中提取节点及其全部内容

php - XSLT:通过变量值选择一个元素

python - 无法在 Python 中下载完整文件

javascript - 如何从 Xpath 中的 javascript 事件处理程序获取链接?