PYTHON3.7升级到3.8版本后,原有requests库使用代理读取https网页提示出错。
出错提示:
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.XXXXX', port=443): Max retries exceeded with url: /33_33268/ (Caused by SSLError(SSLError(1, '[S
SL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1131)')))
多次尝试后,被代理服务器拒绝。
更换3.7版本后,可以正常运行。
在网上查询许久没有找到合适的解决方案;后续又跟踪了库的代码,也没找打解决方案。
最后实在没办法,考虑错误因SSL版本问题引起,在requests申请的时候,将https代理改为http代理,可以解决一部分问题。
修改内容如下:
proxies = requests.utils.getproxies()
if proxies and 'https' in proxies:
proxies['https'] = proxies['http']
page = requests.get(url, timeout=timeout, headers = html_header, proxies = proxies)
以上方案可以解决当前问题,但发现还有https网站提示出错,暂时没有什么办法。我猜测是SSL库的问题,只能等版本更新了。
File "XXXX\Python38\lib\ssl.py", line1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1131)
requests.exceptions.SSLError:
HTTPSConnectionPool(host='www.XXXXXX', port=443):
Max retries exceeded with url: /html/7/7102/ (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1131)')))
备注一下:应该还可以通过抓包的方式,看库和代理的握手过程是怎样的。但时间有限,就不折腾了。