简体   繁体   English

如何使用lxml.html库解析HTML

[英]How to parse HTML using the lxml.html library

Here is the HTML that appears on my site: 这是出现在我的网站上的HTML

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

How can I use lxml.html to grab that? 如何使用lxml.html进行抓取?

Use xpath to find the meta tag by name attribute and get the value of content attribute: 使用xpath通过name属性查找meta标记并获取content属性的值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM