可能な限りシンプルにHTMLの解析が行えるrequests-htmlをインストール(Windows10)

スポンサーリンク

可能な限りシンプルにHTMLの解析が行えるrequests-htmlのインストールについて解説しています。(Windows10)

■Python

今回のPythonのバージョンは、「3.8.5」を使用しています。(Windows10)(pythonランチャーでの確認)

■requests-htmlをインストールする

requests-htmlをインストールを行いますが、今回はpipを経由してインストールを行うので、まずWindowsのコマンドプロンプトを起動します。

pip install requests-html

起動後、上記のコマンドを入力し、Enterキーを押します。

なお、今回は、pythonランチャーを使用しており、Python Version 3.8.5にインストールを行うために、pipを使う場合にはコマンドでの切り替えを行います。

py -3.8 -m pip install -U requests-html

切り替えるために、上記のコマンドを入力し、Enterキーを押します。

Collecting requests-html
Downloading requests_html-0.10.0-py3-none-any.whl (13 kB)
Collecting fake-useragent
Downloading fake-useragent-0.1.11.tar.gz (13 kB)
Requirement already satisfied, skipping upgrade: requests in c:\pg\python38\lib\site-packages (from requests-html) (2.25.0)
Collecting parse
Downloading parse-1.18.0.tar.gz (30 kB)
Collecting pyquery
Downloading pyquery-1.4.3-py3-none-any.whl (22 kB)
Requirement already satisfied, skipping upgrade: w3lib in c:\pg\python38\lib\site-packages (from requests-html) (1.22.0)
Collecting pyppeteer>=0.0.14
Downloading pyppeteer-0.2.2-py3-none-any.whl (145 kB)
|████████████████████████████████| 145 kB 1.7 MB/s
Collecting bs4
Downloading bs4-0.0.1.tar.gz (1.1 kB)
Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in c:\pg\python38\lib\site-packages (from requests->requests-html) (2.10)
Requirement already satisfied, skipping upgrade: urllib3<1.27,>=1.21.1 in c:\pg\python38\lib\site-packages (from requests->requests-html) (1.26.2)
Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in c:\pg\python38\lib\site-packages (from requests->requests-html) (3.0.4)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in c:\pg\python38\lib\site-packages (from requests->requests-html) (2020.11.8)
Requirement already satisfied, skipping upgrade: cssselect>0.7.9 in c:\pg\python38\lib\site-packages (from pyquery->requests-html) (1.1.0)
Requirement already satisfied, skipping upgrade: lxml>=2.1 in c:\pg\python38\lib\site-packages (from pyquery->requests-html) (4.6.1)
Requirement already satisfied, skipping upgrade: six>=1.4.1 in c:\pg\python38\lib\site-packages (from w3lib->requests-html) (1.15.0)
Requirement already satisfied, skipping upgrade: tqdm<5.0.0,>=4.42.1 in c:\pg\python38\lib\site-packages (from pyppeteer>=0.0.14->requests-html) (4.53.0)
Collecting websockets<9.0,>=8.1
Downloading websockets-8.1-cp38-cp38-win_amd64.whl (66 kB)
|████████████████████████████████| 66 kB 2.3 MB/s
Collecting pyee<8.0.0,>=7.0.1
Downloading pyee-7.0.4-py2.py3-none-any.whl (12 kB)
Collecting appdirs<2.0.0,>=1.4.3
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Requirement already satisfied, skipping upgrade: beautifulsoup4 in c:\pg\python38\lib\site-packages (from bs4->requests-html) (4.9.3)
Requirement already satisfied, skipping upgrade: soupsieve>1.2; python_version >= “3.0” in c:\pg\python38\lib\site-packages (from beautifulsoup4->bs4->requests-html) (2.0.1)
Using legacy ‘setup.py install’ for fake-useragent, since package ‘wheel’ is not installed.
Using legacy ‘setup.py install’ for parse, since package ‘wheel’ is not installed.
Using legacy ‘setup.py install’ for bs4, since package ‘wheel’ is not installed.
Installing collected packages: fake-useragent, parse, pyquery, websockets, pyee, appdirs, pyppeteer, bs4, requests-html
Running setup.py install for fake-useragent … done
Running setup.py install for parse … done
Running setup.py install for bs4 … done
Successfully installed appdirs-1.4.4 bs4-0.0.1 fake-useragent-0.1.11 parse-1.18.0 pyee-7.0.4 pyppeteer-0.2.2 pyquery-1.4.3 requests-html-0.10.0 websockets-8.1

Enterキーを押すと、インストールが開始され、「Successfully installed」と表示されます。これが表示されれば、正常にインストールは完了となります。

■実行

完了後、Windowsのコマンドプロンプトを起動し、Pythonのスクリプトを実行してみます。

C:\Users\user>py -3.8 C:\Users\user\test.py
[W:pyppeteer.chromium_downloader] start chromium download.
Download may take a few minutes.
100%|███████████████████████████████████████████████████████████████| 136913619/136913619 [00:23<00:00, 5904628.47it/s]
[W:pyppeteer.chromium_downloader]
chromium download done.

実行すると、オープンソースのウェブブラウザである「Chromium(クロミウム)」のダウンロードが開始され、「chromium download done.(クロミウムのダウンロードが完了しました。)」と表示されます。これでHTMLの解析(Webスクレイピングなど)が行えるようになります。

■「pyppeteer.errors.TimeoutError: Navigation Timeout Exceeded: 8000 ms exceeded.」というエラーの対処

requests-htmlモジュールを使用すると、「pyppeteer.errors.TimeoutError: Navigation Timeout Exceeded: 8000 ms exceeded.」というエラーが表示する場合があります。

このようなエラーの対処としては、「r.html.render(sleep=1)」と設定している場合は、「r.html.render(timeout=20)」と変更すると、エラーが解消されます。「r.html.render()」のデフォルトのタイムアウトは8秒となっています。すべてのJavaScriptコードが8秒以内にロードされない場合、このエラーが発生しますので、タイムアウトの時間を長めに設定します。

コメント

タイトルとURLをコピーしました