Reputation: 236
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?.
Upvotes: 2
Views: 751
Reputation: 473893
Few things are wrong in your xpath:
veg-icon vegIco
, while in the HTML the child div
has veg-icon icon
@
: @id
instead of id
The fixed version:
//div[div[@class="veg-icon icon"]]/@id
Upvotes: 4