【发布时间】:2021-09-07 15:12:45
【问题描述】:
我正在尝试创建一个代理测试器,我已经尝试解决这个问题好几天了,但似乎无法找出问题所在。每次我运行它时,无论代理是什么(我尝试了很多)我都会收到错误
"requests.exceptions.ProxyError: HTTPSConnectionPool(host='advanced.name', port=443):最大重试次数 超出 url: /freeproxy (由 ProxyError('Cannot connect to 代理。', NewConnectionError('
的对象:无法建立新连接: [Errno -2] 名称或服务未知')))"
该消息听起来代理很糟糕,但我已经尝试了一个我知道可以工作的船负载。这是我的代码
import requests
from requests.exceptions import Timeout
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-GB,en;q=0.5',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
}
proxy = {
"http": "http//132.145.54.142:1080",
"https": "https//132.145.54.142:1080"
}
proxy_Test = requests.get("https://advanced.name/freeproxy", headers=headers, proxies=proxy, timeout=10)
print(proxy_Test.status_code)
if proxy_Test.status_code == 200:
print("this proxy is good:" + str(proxy).replace("{", " ").replace("}", "").replace("'", ""))
else:
print("This proxy is no good")
try:
x = requests.get("http://www.google.com", headers=headers, proxies=proxy, timeout=10)
except Timeout:
print("error found")
【问题讨论】:
-
代码中有语法错误。
"http": "http//132.145.54.142:1080"中缺少冒号。应该是"http": "http://132.145.54.142:1080" -
嗯,当我运行它时,它实际上会等待给定的超时时间,但仍然会抛出错误。如果代理不好,它应该只打印“这个代理不好”。我也尝试了大约 10 种不同的代理。
-
由于你代理
132.145.54.142:1080在互联网上可用。我自己尝试。首先你不能将 http 和 https 设置为相同的端口。其次你的代理没有设置好。你可以通过测试curl -v -x https://132.145.54.142:1080 ifconfig.co或其他程序。
标签: python python-requests httprequest