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).