📝 【メモ Python】プロキシエラーとの格闘
作成日: 2022/02/10
2

今日も今日とてPythonと奮闘

今日のエラー

requests.exceptions.ProxyError: HTTPSConnectionPool(host='news.yahoo.co.jp', port=443): Max retries exceeded with url: / (Caused by ProxyError('Your proxy appears to only use HTTP and not HTTPS, try changing your proxy URL to be HTTP. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#https-proxy-error-http-proxy', SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)'))))

参考になりそうなページ

SSL: WRONG_VERSION_NUMBER ON PYTHON REQUEST
プロキシを指定すれば行けそうだったので、例になぞりindex.pyを修正してみる
こちらを参考にターミナルにnetshを入力→winhttpを入力→show proxyを入力することでサーバーのipアドレスを取得★

修正後のindex.py

import requests
proxies = {'https': 'http://プロキシサーバーのIPアドレス'}
response = requests.get('http://news.yahoo.co.jp', verify=False, proxies=proxies)

response.text[:500]

エラーが変わった!!!

C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host '172.16.73.111'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  warnings.warn(

ところでどうしてタイトルにリンクが…?

またまたエラーをググる

【Python】requestsにおけるInsecureRequestWarningを非表示にする

安全なことがわかっているし攻撃の度に警告が出ていては邪魔で速度も落ちるので非表示にしてしまう.以下のようにurllib3のexceptionsを読み込んでおいて,disable_warningsを呼べばOK.
とのことなのでさらにindex.pyを修正

修正後のコード

# resuests モジュールをインポート
import requests
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)
proxies = {'https': 'プロキシサーバーのIPアドレス'}
response = requests.get('http://news.yahoo.co.jp', verify=False, proxies=proxies)

response.text[:500]

実行してみる

PS C:\Users\ユーザー名\Desktop\test_py> py index.py
PS C:\Users\ユーザー名\Desktop\test_py> 

動かない…ただの屍のようだ…
せめてエラーを吐いてよおおおおおおおおおおおおお

もうむり

無理すぎるので部長しゃんにプロキシサーバーの制限ゆるめて~~~って泣きついてきました
それが正しいお願いなのかはわかりまてん
なんでもいいからやらせてええええ


アプリを作ってみたいウェブデザイナー。