Sindre Smistad
Sindre Smistad

Reputation: 139

Python lxml, matching attributes

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"))

Upvotes: 2

Views: 1276

Answers (2)

Frank Fang
Frank Fang

Reputation: 1109

You could use xpath with lxml, here's the example:

f = StringIO(xmlString)
tree = etree.parse(f)
r = tree.xpath('/input[@name="myInput"]/@value')

See this document: http://lxml.de/xpathxslt.html

Upvotes: 2

unutbu
unutbu

Reputation: 879799

You could do it with an XPath:

import lxml.html as LH

content='<input name="myInput" value="This is what i want"/>'

doc=LH.fromstring(content)
for val in doc.xpath("//input[@name='myInput']/@value"):
    print(val)

yields

This is what i want

The XPath used above has the following meaning:

    //input                    # find all input tags
      [@name='myInput']        # such that the name attribute equals myInput
      /@value                  # return the value of the value attribute           

Upvotes: 3

Related Questions