热门问题
时间线
聊天
视角

Beautiful Soup

Python的HTML/XML解析程式 来自维基百科,自由的百科全书

Remove ads

Beautiful Soup是一个Python包,功能包括解析HTMLXML文档、修复含有未闭合标签等错误的文档(此种文档常被称为tag soup)。这个扩展包为待解析的页面建立一棵,以便提取其中的数据,这在网络数据采集时非常有用。[2]

事实速览 原作者, 当前版本 ...

在2021年,Python 2.7的官方支持终止,BeautifulSoup发行版4.9.3是支持Python 2.7的最后版本[3]

Remove ads

示例代码

#!/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', '/'))

参见

参考资料

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.

Remove ads