I’m having some troubles wrapping my head around lxml. I have some html I want to parse, and I managed to do it, but it doesn’t feel like the best way to do it.
I want to extract the value of the value attribute, but only if the value of name is “myInput”
<input name="myInput" value="This is what i want"/>
I manage to do this, but I feel there is a better solution.
doc = html.fromstring(data)
tr = doc.cssselect("input")
for x in tr:
if x.get("name") == "myInput":
print(x.get("value"))
You could do it with an XPath:
yields
The XPath used above has the following meaning: