0
点赞
收藏
分享

微信扫一扫

Python爬虫:使用lxml解析网页内容

那小那小 2022-02-17 阅读 30


安装

pip install lxml

代码示例

from lxml import etree

text = """
<html>
<head>
<title>这是标题</title>
</head>
<body>
<div>这是内容</div>
</body>
</html>"""

html = etree.HTML(text)

# 使用xpath解析
titles = html.xpath("//title")
for title in titles:
print(title.text)

# 使用css解析
titles = html.cssselect("title")
for title in titles:
print(title.text)



举报

相关推荐

0 条评论