Beautiful Soup是一个Python包,功能包括解析HTML、XML文档、修复含有未闭合标签等错误的文档(此种文档常被称为tag soup)。这个扩展包为待解析的页面建立一棵树,以便提取其中的数据,这在网络数据采集时非常有用。。
示例代码
#!/usr/bin/env python3
Anchor extraction from HTML document
from bs4 import BeautifulSoup
from urllib.request import urlopen
with urlopen('https://en.wikipedia.org/wiki/Main_Page') as response:
soup = BeautifulSoup(response, 'html.parser')
for anchor in soup.find_all('a'):
print(anchor.get('href', '/'))
参见
- HTML解析器对比
参考资料
评论 (0)