Beautiful Soup

Python的HTML/XML解析程式

Beautiful Soup是一個Python包,功能包括解析HTMLXML文檔、修復含有未閉合標籤等錯誤的文檔(此種文檔常被稱為tag soup)。這個擴展包為待解析的頁面建立一棵,以便提取其中的數據,這在網絡數據採集時非常有用。[2]

Beautiful Soup
原作者Leonard Richardson
當前版本
  • 4.12.3 (2024年1月17日;穩定版本)[1]
編輯維基數據鏈接
源代碼庫 編輯維基數據鏈接
編程語言Python
類型HTML解析庫、網絡數據採集
許可協議Python軟件基金會許可證 (Beautiful Soup 3及以前)
MIT許可證(Beautiful 4及以後)[2]
網站www.crummy.com/software/BeautifulSoup/

在2021年,Python 2.7的官方支持終止,BeautifulSoup發行版4.9.3是支持Python 2.7的最後版本[3]

示例代碼

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

參見

編輯

參考資料

編輯
  1. ^ https://git.launchpad.net/beautifulsoup/tree/CHANGELOG; 檢索日期: 2024年1月18日.
  2. ^ 2.0 2.1 Beautiful Soup website. [18 April 2012]. (原始內容存檔於2017-02-03). Beautiful Soup is licensed under the same terms as Python itself 
  3. ^ Richardson, Leonard. Beautiful Soup 4.10.0. beautifulsoup. Google Groups. 7 Sep 2021 [27 September 2022]. (原始內容存檔於2022-09-29).