Search code examples
pythoncssxpathattributesxpath-2.0

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

but it fails to provide the attribute value. The output is none with the above XPath. I need to get the image_url from this page. I have tried different XPath expressions but the result is none with all the expression.


Solution

  • To get all image attributes, you have to use the correct XPath :

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

    Output :

    ['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']
    

    To get just the first one, use :

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