带有Python“请求”模块的代理(不工作)

2024-09-26 18:05:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个代理测试员,我已经尝试了好几天解决这个问题,但似乎无法解决这个问题。每次我运行它时,无论代理是什么(我已经尝试了很多次),我都会得到错误

"requests.exceptions.ProxyError: HTTPSConnectionPool(host='advanced.name', port=443): Max retries exceeded with url: /freeproxy (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f9a7e0ea400>: Failed to establish a new connection: [Errno -2] Name or service not known')))"

消息听起来像是代理坏了,但我已经尝试了一个船负载,我知道工作。这是我的密码

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")

Tags: tonamehttpstesthttp代理requestsreplace
2条回答

此网站在您所在的国家/地区进行筛选,您设置的代理:

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

也不起作用,因此您必须找到另一个代理

如果要隐藏ip地址,可以使用TorreRequest而不是请求:

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

相关问题 更多 >

    热门问题