首页 > 解决方案 > 使用 xpath 获取图像

问题描述

我一直在尝试从该网站的滑动条获取图像:https ://propertyfox.co.za/property/pf10434/我似乎无法访问该课程,因此我决定检查页面源。但是,我仍然在挣扎,不知道我做错了什么

这就是我现在拥有的代码的样子,但我总是得到一个空数组:

images = response.xpath('//*[@class="pagination-item"]//*[has-class("total-slide")]/span/text()').extract()

有什么建议么?谢谢!

标签: pythonxpathweb-scrapingscrapy

解决方案


在页面源中,您可以看到图像链接存储在id="galleryatts". 以下 xpath 获取图像:

images = response.xpath('//*[@id="galleryatts"]/@data-allimg').extract_first()

这样你就可以得到一个字符串中的所有图像,用分号分隔。要将图像作为列表获取,您只需执行以下操作:

image_urls = images.split(';')

推荐阅读