Getting parent tag id with lxml

755 Views Asked by At

I am trying scrape a dummy site and get the parent tag of one that I am searching for. Heres the structure of the code I am searching for:

<div id='veg1'>
    <div class='veg-icon icon'></div>
</div>

<div id='veg2'>
</div>    

Heres my python script:

from lxml import html
import requests

req = requests.get('https://mysite.com')
vegTree = html.fromstring(req.text)
veg = vegTree.xpath('//div[div[@class="veg-icon vegIco"]]/id')

When veg is printed I get an empty list but I am hoping to get veg1. As I am not getting an error I am not sure what has gone wrong. As I was it in a previous question and followed that syntax. See lxml: get element with a particular child element?.

1

There are 1 best solutions below

0
On BEST ANSWER

Few things are wrong in your xpath:

  • you are checking for the classes veg-icon vegIco, while in the HTML the child div has veg-icon icon
  • attributes are prepended with @: @id instead of id

The fixed version:

//div[div[@class="veg-icon icon"]]/@id