[Python 100日チャレンジ] Day 53 - BeautifulSoupの基本
BeautifulSoupでHTMLを解析し、タイトル、リンク、本文など必要な要素を取り出す基本を学びます。
[Python 100日チャレンジ] Day 53 - BeautifulSoupの基本
BeautifulSoupとは
BeautifulSoupは、HTMLやXMLを解析するためのライブラリです。Webスクレイピングでよく使われます。
1
2
3
4
5
from bs4 import BeautifulSoup
html = "<html><h1>Hello</h1></html>"
soup = BeautifulSoup(html, "html.parser")
print(soup.find("h1").text)
requestsと組み合わせる
1
2
3
4
5
import requests
from bs4 import BeautifulSoup
response = requests.get("https://example.com")
soup = BeautifulSoup(response.text, "html.parser")
今日のゴール
BeautifulSoupを使って、HTMLから必要な要素を探せるようになりましょう。まずは find、find_all、.text を使います。
この投稿は投稿者によって CC BY 4.0 の下でライセンスされています。
