2

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?.

Community
  • 1
  • 1
user2157179
  • 238
  • 2
  • 4
  • 19

1 Answers1

4

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
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195