美文网首页
Python中HTML解析

Python中HTML解析

作者: ZoranLee | 来源:发表于2020-05-22 20:01 被阅读0次

BeautifulSoup

安装

pip install beautifullsoup4

使用

from bs4 import BeautifulSoup;

soup = BeautifulSoup(html);

ul = soup.find('ul',attrs={'class':'county'}); //找HTML中class为county 的元素
ul.find('li');// ul节点下找第一个li节点

更多

https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/

Lxml

  • 解析速度比Beautiful Soup更快

安装

pip install lxml  

使用

import lxml.html;
import lxml.cssselect;

tree = lxml.html.fromstring(html);
result = lxml.html.tostring(tree,pretty_print=True); //格式化输出
print result

td = tree.cssselect('tr#places_area__row > td.w2p_fw ')[0]//按节点找
print td.text_content()

相关文章

网友评论

      本文标题:Python中HTML解析

      本文链接:https://www.haomeiwen.com/subject/shkiohtx.html