木三136 2021-03-24 11:27 采纳率: 78.9%
浏览 290
已采纳

python 使用代理访问网站遇到的问题

源码如下

import requests

# 代理ip请求测试网站
url = "http://myip.ipip.net/"
# 封装请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'}

# 封装代理ip参数值:协议/ip/端口
proxy = {'http': 'http//117.91.249.179'}
# 发送请求
response = requests.get(url=url, headers=headers, proxies=proxy, timeout=10)
print(response.status_code)  # 打印请求返回的状态码
# 获取响应数据
response_data = response.text
# 把请求之后的获取到的响应数据保存到本地文件夹,以查看代理请求是否成功!
with open('ip.html', 'w', encoding='utf-8') as adi:
    adi.write(response_data)
exit()  # 代码执行完毕退出程序

D:\Learn\PyCharm\项目制作_1\venv\Scripts\python.exe D:/Learn/PyCharm/项目制作_1/火车票/test2.py
Traceback (most recent call last):
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "D:\Learn\Anaconda3\envs\python35\lib\socket.py", line 733, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connection.py", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "D:\Learn\Anaconda3\envs\python35\lib\http\client.py", line 1107, in request
    self._send_request(method, url, body, headers)
  File "D:\Learn\Anaconda3\envs\python35\lib\http\client.py", line 1152, in _send_request
    self.endheaders(body)
  File "D:\Learn\Anaconda3\envs\python35\lib\http\client.py", line 1103, in endheaders
    self._send_output(message_body)
  File "D:\Learn\Anaconda3\envs\python35\lib\http\client.py", line 934, in _send_output
    self.send(msg)
  File "D:\Learn\Anaconda3\envs\python35\lib\http\client.py", line 877, in send
    self.connect()
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connection.py", line 200, in connect
    conn = self._new_conn()
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connection.py", line 182, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001A2D5FAC0F0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\urllib3\util\retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: http://myip.ipip.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001A2D5FAC0F0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Learn/PyCharm/项目制作_1/火车票/test2.py", line 14, in <module>
    response = requests.get(url=url, headers=headers, proxies=proxy, timeout=10)
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "D:\Learn\PyCharm\项目制作_1\venv\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='http', port=80): Max retries exceeded with url: http://myip.ipip.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001A2D5FAC0F0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)))

我猜出问题的原因是这个,但是不知道怎么解决他,有没有大佬帮帮咱,谢谢啦

 

  • 写回答

2条回答 默认 最新

  • liqiang94 2021-03-24 11:36
    关注

    代理没有设置端口

    # 端口要配置,否则默认80,一般代理端口不会是80的
    proxy = {'http': 'http//ip:port'}
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题