1

Here is the HTML that appears on my site:

<meta content="auth" name="param" />
<meta content="I_WANT_THIS" name="token" />

How can I use lxml.html to grab that?

1 Answer 1

2

Use xpath to find the meta tag by name attribute and get the value of content attribute:

from lxml.html import fromstring


html_data = """ <meta content="auth" name="param" />
 <meta content="I_WANT_THIS" name="token" />"""

tree = fromstring(html_data)
print tree.xpath('//meta[@name="token"]/@content')

prints:

['I_WANT_THIS']
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain what ".//meta[@name="token"]/@content'" does?
@user3412816 yup, it is an xpath expression that basically says: find me meta tag anywhere in the html, this meta tag show have an attribute name with the value token, then give me the value of content attrubite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.