起因
本来打算爬取p站上的图片,因此不得不开vpn代理,结果发现了如下错误:
于是在网上查了半天SSL的错误,发现其实并不是关键,不然为什么不开代理就行,开代理就行呢,于是以requests SSL proxy
为关键字重新进行了查找,最后在# Python 遭遇 ProxyError 问题记录。
原来根源在urllib库对于代理的处理,https代理本质还是用http发出的请求:
解决
首先测试在没开代理之前的系统代理:空
再测试在开启vpn之后的系统代理:
按照之前的文章,urllib库的https代理的url必须配置成 'www.xxx.com' 的格式:
import requests;
from urllib.request import getproxies;
# when opening the vpn, call this function
def get_correct_proxies():
headers = getproxies();
try:
if(len(headers) <= 0):
return {};
if(headers.get('https') is not None):
headers['https'] = headers['https'].replace('https', 'http');
return headers;
except Exception:
return {};
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54',
};
proxies = get_correct_proxies();
response = requests.get('https://www.baidu.com',headers=headers);
open('baidu.html', 'w',encoding='utf-8').write(response.text);