我正在抓这个网站
http://www.propertyfinder.ae/en/buy/villa-for-sale-dubai-jumeirah-park-1849328.html?img/0

我想获得此标签div[@id='propertyPhoto']中的所有图像src

我试过这个xpath

.//div[@id='propertyPhoto']//img/@src


然后我循环执行以提取src,但是我只有第一个图像src

请帮助

最佳答案

div#propertyPhoto中只有主图像。其他在li#propertyPhotoMini0li#propertyPhotoMini1等内部

因此,XPath应该稍作修改以匹配两者。它们的id属性都以propertyPhoto开头;您可以使用以下XPath:

.//*[starts-with(@id, 'propertyPhoto')]//img/@src


例:

import urllib
from scrapy.selector import Selector
url = 'http://www.propertyfinder.ae/en/buy/villa-for-sale-dubai-jumeirah-park-1849328.html?img/0'
h = urllib.urlopen(url).read()
root = Selector(text=h, type='html')
for url in root.xpath(".//*[starts-with(@id, 'propertyPhoto')]//img/@src").extract():
    print(url)


输出:

http://c1369023.r23.cf3.rackcdn.com/1849328-1-wide.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-1-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-2-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-3-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-4-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-5-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-6-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-7-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-8-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-9-mini.jpg
http://c1369023.r23.cf3.rackcdn.com/1849328-10-mini.jpg

08-04 02:55