源码如下
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',)))
我猜出问题的原因是这个,但是不知道怎么解决他,有没有大佬帮帮咱,谢谢啦