热门问题
时间线
聊天
视角
Beautiful Soup
Python的HTML/XML解析程式 来自维基百科,自由的百科全书
Remove ads
Beautiful Soup是一個Python包,功能包括解析HTML、XML文件、修復含有未閉合標籤等錯誤的文件(此種文件常被稱為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', '/'))
參見
參考資料
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads