书上提供的原始代码:
import requests
# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"状态码:{r.status_code}")
# 将API响应赋给一个变量
response_dict = r.json()
# 处理结果
print(response_dict.keys())
报错信息:
Traceback (most recent call last):
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connectionpool.py", line 777, in urlopen
self._prepare_proxy(conn)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connectionpool.py", line 1046, in _prepare_proxy
conn.connect()
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connection.py", line 619, in connect
self.sock = sock = self._connect_tls_proxy(self.host, sock)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connection.py", line 692, in _connect_tls_proxy
tls_in_tls=False,
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connection.py", line 793, in _ssl_wrap_socket_and_match_hostname
tls_in_tls=tls_in_tls,
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\util\ssl_.py", line 471, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\util\ssl_.py", line 515, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "D:\Program\Python\Python37\lib\ssl.py", line 423, in wrap_socket
session=session
File "D:\Program\Python\Python37\lib\ssl.py", line 870, in _create
self.do_handshake()
File "D:\Program\Python\Python37\lib\ssl.py", line 1139, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
The above exception was the direct cause of the following exception:
urllib3.exceptions.ProxyError: ('Unable to connect to proxy', OSError(0, 'Error'))
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\adapters.py", line 497, in send
chunked=chunked,
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\connectionpool.py", line 846, in urlopen
method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2]
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\urllib3\util\retry.py", line 515, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /search/repositories?q=language:python&sort=stars (Caused by ProxyError('Unable to connect to proxy', OSError(0, 'Error')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Code\pyCharm\chapter_17\python_repos.py", line 6, in <module>
r = requests.get(url, headers=headers)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\api.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "D:\Code\pyCharm\chapter_17\venv\lib\site-packages\requests\adapters.py", line 513, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /search/repositories?q=language:python&sort=stars (Caused by ProxyError('Unable to connect to proxy', OSError(0, 'Error')))
解决办法:
修改代码,配置代理服务器
import requests
# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
# 配置代理服务器(注意:具体IP和端口,根据自身情况自行修改)
proxies = {
'http': 'http://127.0.0.1:7890',
'https': 'http://127.0.0.1:7890',
}
r = requests.get(url, headers=headers, proxies=proxies)
print(f"状态码:{r.status_code}")
# 将API响应赋给一个变量
response_dict = r.json()
# 处理结果
print(response_dict.keys())
标签:API,chapter,lib,17,py,Unable,报错,File,line From: https://www.cnblogs.com/infocodez/p/18202707