import requests
proxy = ‘127.0.0.1:9999’
proxies = {
“http”: proxy,
# “https”: “https://” + proxy
}
try:
url = ‘http://httpbin.org/get’
url = ‘https://www.google.com’
response = requests.get(url=url, headers=None, proxies=proxies, verify=False)
print(response.text)
except requests.exceptions.ConnectionError as e:
print(‘Error’, e.args)
第一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,就啥事都没有。。。。很无语,还是我个人品问题?不过以前这模块都是好好的啥就忽然不行了。。。
import requests
proxy = 'http://127.0.0.1:9999'
proxies = {
"http": proxy,
"https": proxy,
}
try:
url = 'https://www.google.com'
response = requests.get(url=url, headers=None, proxies=proxies, verify=False)
print(response.text)
except requests.exceptions.ConnectionError as e:
print('Error', e.args)
如果不行,那就是你代理的问题了
居然又变了代理设置,现在没有问题。
不过新问题又来了
Please Wait... | Cloudflare
所谓5秒问题搜索一下都说要另安装其它模拟,真奇怪request不是封装urllib.request吗?直接用urllib.request
#! /usr/bin/env python3
# __author__=wei
import urllib.parse
import ssl
import 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’: ‘[email protected]’, ‘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”))