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

I want to get the attribute value of @src using XPath in python

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 的输出是 none。我需要从此页面获取 image_url。我尝试了不同的 XPath 表达式,但结果是 none 所有表达式。

要获取所有图像属性,您必须使用正确的 XPath :

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()