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

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?