Beauful Soup (HTML パーサー)

出典: フリー百科事典『ウィキペディア(Wikipedia)』

Beautiful Soup は、HTMLXMLといったマークアップ言語の文書を構文解析するためのPythonパッケージである。ドキュメントから構築された解析木ウェブスクレイピングに有用である[1][2]

Beautiful Soupはレオナルド・リチャードソンによって開始された。彼はプロジェクトへの貢献を続けている[3]。また、オープンソースソフトウェアを管理するTideliftによっても支えられている[4]

コード例[編集]

以下の例では、Pythonの標準ライブラリである urllib[5] を用いて、ウィキペディアのメインページを読み込み、Beautiful Soupで構文解析し、全てのハイパーリンクを得る。

#!/usr/bin/env python3
# HTML文書からのハイパーリンクの抽出
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', '/'))

歴史[編集]

Beautiful Soupは不思議の国のアリス[6]の詩とtag soup[7]の両方にちなんで名づけられた。

2006年4月から2012年3月までは、Beautiful Soup 3 がリリースされていた。 最新版の Beautiful Soup 4.xpip install beautifulsoup4からインストールできる。

2021年に、Python 2.7 のサポートが終了し、 Beautiful Soup 4.9.3 がPython 2.7をサポートする最後のバージョンとなった[8]

脚注[編集]

  1. ^ Hajba, Gábor László (2018), Hajba, Gábor László, ed., “Using Beautiful Soup” (英語), Website Scraping with Python: Using BeautifulSoup and Scrapy (Apress): 41–96, doi:10.1007/978-1-4842-3925-4_3, ISBN 978-1-4842-3925-4 
  2. ^ Python. “Beautiful Soup: Build a Web Scraper With Python – Real Python” (英語). realpython.com. 2023年6月1日閲覧。
  3. ^ Code : Leonard Richardson” (英語). Launchpad. 2020年9月19日閲覧。
  4. ^ Tidelift. “beautifulsoup4 | pypi via the Tidelift Subscription” (英語). tidelift.com. 2020年9月19日閲覧。
  5. ^ Python. “Python's urllib.request for HTTP Requests – Real Python” (英語). realpython.com. 2023年6月1日閲覧。
  6. ^ makcorps (2022年12月13日). “BeautifulSoup tutorial: Let's Scrape Web Pages with Python” (英語). 2024年1月24日閲覧。
  7. ^ Python Web Scraping” (英語). Udacity (2021年2月11日). 2024年1月24日閲覧。
  8. ^ Richardson (2021年9月7日). “Beautiful Soup 4.10.0” (英語). beautifulsoup. Google Groups. 2022年9月27日閲覧。