我想在python中使用XPath获得@src的属性值

问题描述 投票:0回答:1

url = link

response.css("div.image-wrap.fff-pic img").xpath("@src").extract()
response.xpath("//div[@class='image-wrap fff-pic']/img/@src").extract()

但无法提供属性值。上面的XPath没有输出。我需要从此页面获取image_url。我尝试了不同的XPath表达式,但是所有表达式都没有结果。

python css xpath attributes xpath-2.0
1个回答
0
投票
response.xpath("//div[@class='image-wrap']/img/@data-src").extract()

输出:

['https://i.dailymail.co.uk/1s/2020/04/26/01/27654346-8257569-image-a-12_1587860534559.jpg',
 'https://i.dailymail.co.uk/1s/2020/04/26/01/27654344-8257569-Few_pieces_The_33_year_old_actor_wore_only_hot_pink_athletic_sho-a-55_1587861487279.jpg',
 'https://i.dailymail.co.uk/1s/2020/04/26/01/26558472-8257569-Back_on_Shia_seems_to_have_reconciled_with_his_ex_Mia_Goth_pictu-a-56_1587861487300.jpg']

要获得第一个,请使用:

response.xpath("(//div[@class='image-wrap'])[1]/img/@data-src").extract()
© www.soinside.com 2019 - 2024. All rights reserved.