第一url,就是测试不用代理都可以访问而且已经显示代理正常
一换第二个url访问google之类,就马上错误提示
Error (MaxRetryError(“HTTPSConnectionPool(host=‘www.google.com’, port=443): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x000001905920FC48>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。’))”),)
Process finished with exit code 0
以为是代理问题,结果一换成
urllib.request,就啥事都没有。。。。很无语,还是我个人品问题?不过以前这模块都是好好的啥就忽然不行了。。。
SETTING_PROXY = 1
HTTPS = 1
if HTTPS:
url = ‘https://somesite’
else:
url = ‘http://www.baidu.com/’
user_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)’
values = {‘name’: ‘xxx1’, ‘email’: ‘fuck@qq.com’, ‘password’: ‘123456’}
headers = {‘User-Agent’: user_agent}
if SETTING_PROXY:
ssl._create_default_https_context = ssl._create_unverified_context # python3中使用urlopen()报错的解决方法_悠闲独自在的博客-CSDN博客_urlopen error
if HTTPS:
proxy_support = urllib.request.ProxyHandler({“https”: “172.0.0.1:9999”})
else:
proxy_support = urllib.request.ProxyHandler({“http”: “172.0.0.1:9999”})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
data = urllib.parse.urlencode(values).encode(
encoding=‘UTF8’) # if you don’t encode utf, TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
req = urllib.request.Request(url, data=None, headers=headers)
response = urllib.request.urlopen(req)
the_page = response.read()
print(the_page.decode(“utf8”))