BeautifulSoupとSelenium

Python

要素の取得

title = bs.find(id="ttl")
title = bs.find_all('div', class_='ttl')
title = bs.select('div.ttl')
print(title.string)

BeautifulSoupとSeleniumの違い

Reactのように、Javascriptによって動的にページを生成する場合、サーバーにHTTPリクエストして得られるのは<div id=”root”></div>の空タグだけなので、BeautifulSoupではデータ抽出できません。

その場合はSeleniumのWebDriverを使って、最終的なHTMLを読み込んでデータを取得する必要があります。

BeautifulSoupとSeleniumの併用

SeleniumでHTMLコードを取得してからBeautifulSoupで必要なデータを抽出します

Seleniumのinstall

pip install selenium
pip install chromedriver-binary==ブラウザのバージョン114.0.5735.134

BACK