Boris Boris - 1 year ago 184
Python Question

Parsing part of an input tag with lxml and Python

I have an input-tag looking like this:

<input name="sheet" value="5B" tabindex="994" data-enpassid="__11" type="submit">


No I want to get only the content given to the value-attribute (5B in this case). Is this possibe using lxml and if so, how?

Answer Source

Try this. I've created two expressions using css selector and xpath. They both will give you 5B as result:

html='''
<input name="sheet" value="5B" tabindex="994" data-enpassid="__11" type="submit">
'''
from lxml.html import fromstring

root = fromstring(html)
item = root.cssselect("input")[0].attrib['value']
item1 = root.xpath("//input/@value")[0]
print(item, item1)

Result:

5B, 5B
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download