エックスサーバーで動くPHPプログラムからCloudflare利用サイトにアクセスできないことがあった。
Pythonに cloudscraper というパッケージがあるようなので使ってみる。
エックスサーバーはpipが使えないので、venvでやってみる。
参考サイト:シン・レンタルサーバーでPythonの仮想環境を作成してpipを使う
まとめ
- cloudscraper動かず諦めた。
venv作成
$ python3 -m venv --without-pip venv
venv有効化
$ source venv/bin/activate
(venv)$
pipインストール
(venv)$ wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
(venv)$ python get-pip.py
cloudscraper
(venv)$ pip install cloudscraper
(venv)$ vi cloud_scraper.py
:
(公式サンプルコードをCloudflareサイトURLにしたファイルを作る)
(venv)$ python cloud_scraper.py
DEPRECATION: The OpenSSL being used by this python install (OpenSSL 1.0.2k-fips 26 Jan 2017) does not meet the minimum supported version (>= OpenSSL 1.1.1) in order to support TLS 1.3 required by Cloudflare, You may encounter an unexpected Captcha or cloudflare 1020 blocks.
Traceback (most recent call last):
File "cloud_scraper.py", line 4, in <module>
scraper = cloudscraper.create_scraper()
File "/home/{user}/python3/venv/lib64/python3.6/site-packages/cloudscraper/__init__.py", line 319, in create_scraper
scraper = cls(**kwargs)
File "/home/{user}/python3/venv/lib64/python3.6/site-packages/cloudscraper/__init__.py", line 173, in __init__
ssl_context=self.ssl_context
File "/home/{user}/python3/venv/lib64/python3.6/site-packages/cloudscraper/__init__.py", line 77, in __init__
self.ssl_context.orig_wrap_socket = self.ssl_context.wrap_socket
AttributeError: 'SSLContext' object has no attribute 'orig_wrap_socket'
OpenSSLが古くてダメなのか。。
新しいOpenSSLをエックスサーバーで使う方法を試行錯誤するか・・
うーんそれよりMinicondaを使う方がいいのでは・・
venv無効化
(venv)$ deactivate
venv削除
$ rm -rf venv/