【问题标题】:Proxies with Python 'Requests' module (Not Working)带有 Python 'Requests' 模块的代理(不工作)
【发布时间】: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


【解决方案1】:

如果你想隐藏你的ip地址,你可以使用torrequest代替requests:

pip install torrequest

示例代码(more here):

from torrequest import TorRequest

with TorRequest() as tr:
    response = tr.get('http://ipecho.net/plain')
    print(response.text)

    tr.reset_identity()

    response = tr.get('http://ipecho.net/plain')
    print(response.text)

你需要先安装Tor browser

【讨论】:

  • 我想用它来创建帐户等等。 Tor 请求将被拒绝
【解决方案2】:

此网站已在您所在的国家/地区过滤,并且您设置的代理:

proxy = {     "HTTPS": "http://95.66.151.101:8080" }

也没有成功,所以你必须找到另一个代理

【讨论】:

  • 不,他们肯定工作,他们来自美国,他们应该工作。
  • 你确定吗?那我们看看这里有什么问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 2022-01-19
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
相关资源
最近更新 更多