user3412816
user3412816

Reputation: 53

How to parse HTML using the lxml.html library

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?

Upvotes: 1

Views: 1133

Answers (1)

alecxe
alecxe

Reputation: 474171

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']

Upvotes: 2

Related Questions