hack のためのネタ帳, etc,,,

×

状況

2019-12-18 現在、「Python スクレイピング」で検索すると上位 10 件には、以下のようなライブラリ等が挙がっていた。
  • urllib
  • requests
  • bs4 (Beautiful Soup 4)
  • Selenium
  • pandas
  • Scrapy

また、「Python スクレイピング DOM」以下のライブラリも挙がっていた。
  • PyQuery

Selenium は実ブラウザ利用するタイプなのでとりあえず置いておくとして、
urllib と requests が、http/https のクローリング用、
bs4, PyQuery は DOM っぽく操作するためのパーサーと API
pandas は、データ解析用のライブラリだけどスクレイピングにも使えるとの事
Scrapy はちょっと独特と言うか、スクレイピングフレームワークなんだそうで、ぱっとみ大仰な感じ
システマティックにやる場合には良いのか?

クロール用ライブラリ

urllib は標準で利用可能
requests は Cygwin の公式リポジトリに各バージョン用のがある。
現在 Cygwin で標準なのは Python 3.6 なので以下のパッケージが標準。
  • python36-requests

これはどっちでもそんなに違いはない。
urllib なら
from urllib import request

r = request.urlopen("https://google.com/")
print(r)
requests なら
import requests

r = requests.get("https://google.com/")
print(r)

urllib は標準で入ってるのでそっちで良いんじゃね?って感じかな?

bs4 (Beautiful Soup 4)

Cygwin の公式リポジトリから以下のパッケージが取得できる。
  • python36-bs4

しかし、bs4 (Beautiful Soup 4) を読み込むと以下のような warning が発生する。
>>> import bs4
/usr/lib/python3.6/site-packages/bs4/element.py:16: UserWarning: The soupsieve package is not installed. CSS selectors cannot be used.
  'The soupsieve package is not installed. CSS selectors cannot be used.'

そしてなぜか、soupsieve は Cygwin の公式リポジトリには含まれていない。
従って、pip3 を用いて別途インストールする必要がある。
$ pip3 install soupsieve
Collecting soupsieve
  Downloading https://files.pythonhosted.org/packages/81/94/03c0f04471fc245d08d0a99f7946ac228ca98da4fa75796c507f61e688c2/soupsieve-1.9.5-py2.py3-none-any.whl
Installing collected packages: soupsieve
Successfully installed soupsieve-1.9.5
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
upgrade を推奨されているが、確か、Cygwin で pip3 を upgrade すると package で導入したのを上書きされて困ったような記憶があるので、今回は放置することにした。

PyQuery

此方は、Cygwin の公式リポジトリに収録されていなかった。
要 pip3 install
$ pip3 install pyquery
Collecting pyquery
  Downloading https://files.pythonhosted.org/packages/78/43/95d42e386c61cb639d1a0b94f0c0b9f0b7d6b981ad3c043a836c8b5bc68b/pyquery-1.4.1-py2.py3-none-any.whl
Collecting cssselect>0.7.9 (from pyquery)
  Downloading https://files.pythonhosted.org/packages/3b/d4/3b5c17f00cce85b9a1e6f91096e1cc8e8ede2e1be8e96b87ce1ed09e92c5/cssselect-1.1.0-py2.py3-none-any.whl
Requirement already satisfied: lxml>=2.1 in /usr/lib/python3.6/site-packages (from pyquery) (4.3.2)
Installing collected packages: cssselect, pyquery
Successfully installed cssselect-1.1.0 pyquery-1.4.1
WARNING: You are using pip version 19.2.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます