LoginSignup
8
4

More than 1 year has passed since last update.

【Python】JavaScriptレンダリングページからhtmlのtableをスクレイピングする(Playwright→BeautifulSoup4→pandas)

Last updated at Posted at 2022-03-12

目次

1. はじめに
2. 環境
3. インストールするライブラリ
4. サンプルコード
5. 補足
6. 参考にしたサイト

1. はじめに

Pythonのpandasでread_html()だけを使いWebスクレピングしてると、必ず邪魔をしてくるJavaScriptレンダリングページ。
そんな時、データ処理はpandasにまかせて、htmlのtableデータ取得はPlaywright、BeautifulSoup4を必要最低限だけ利用してスクレイピングする方法です。

2. 環境

  • Windows10 Pro:21H2
  • Python:3.10.5

3. インストールするライブラリ

  • playwright:ブラウザ操作(Selenium→puppeteer→Playwrightときて、これが一番安定)
  • beautifulsoup4:htmlパーサー
  • lxml:htmlパーサー(beautifulsoup4で利用)
  • pandas:データ解析ライブラリ
pip install pandas 
pip install beautifulsoup4
pip install lxml
pip install playwright
playwright install

4. サンプルコード

get_html.py
import pandas as pd
from bs4 import BeautifulSoup
from playwright.sync_api import sync_playwright

# playwright→BeautifulSoup4→pandasでHTMLのtableを読み込む
def get_html(playwright):
 try:
  # playwright
  browser = playwright.chromium.launch()
  context  = browser.new_context()
  page = context.new_page()
  page.goto("https://スクレイピングしたいWebサイトのURL")
  html = page.content()
  # BeautifulSoup4
  soup = BeautifulSoup(html, "lxml")
  tables = soup.find_all("table")
  # pandas
  dfs = pd.read_html(str(tables))

 except:
  #エラー処理を書く(HTTPエラー、tableタグなし等)
  print("Error")

 else:
  # pandasのデータフレーム処理を書く
  print(dfs)

 finally:
  page.close()
  context.close()
  browser.close()

# get_html関数呼び出し
with sync_playwright() as playwright:
  get_html(playwright)

5. 補足

PuppeteerからPlaywrightに変更し非同期処理をやめたら下記エラーは発生しなくなった

警告
ConnectionResetError: [WinError 10054] 既存の接続はリモート ホストに強制的に切断されました。

6. 参考にしたサイト

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4