Python HackerNews API 连接问题
林登万 2018-07-24 10:47:37 目前学到了Python Crash Course 的API这一部分,我按照教材的Hacker News API 代码输入
import requests
from operator import itemgetter
# Make an API call, and store the response.
url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
r = requests.get(url)
print("Status code:", r.status_code)
# Process information about each submission.
submission_ids = r.json()
submission_dicts = []
for submission_id in submission_ids[:30]:
# Make a separate API call for each submission.
url = ('https://hacker-news.firebaseio.com/v0/item/' +
str(submission_id) + '.json')
submission_r = requests.get(url)
print(submission_r.status_code)
response_dict = submission_r.json()
submission_dict = {
'title': response_dict['title'],
'link': 'http://news.ycombinator.com/item?id=' + str(submission_id),
'comments': response_dict.get('descendants', 0)
}
submission_dicts.append(submission_dict)
submission_dicts = sorted(submission_dicts, key=itemgetter('comments'),
reverse=True)
for submission_dict in submission_dicts:
print("\nTitle:", submission_dict['title'])
print("Discussion link:", submission_dict['link'])
print("Comments:", submission_dict['comments'])
,但是系统没有给与正确的反馈,给的反馈如下
Traceback (most recent call last):
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connection.py", line 171, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\util\connection.py", line 79, in create_connection
raise err
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\util\connection.py", line 69, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connectionpool.py", line 849, in _validate_conn
conn.connect()
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connection.py", line 314, in connect
conn = self._new_conn()
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connection.py", line 180, in _new_conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x000002257BBCA978>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\requests\adapters.py", line 445, in send
timeout=timeout
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\urllib3\util\retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='hacker-news.firebaseio.com', port=443): Max retries exceeded with url: /v0/topstories.json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000002257BBCA978>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "hn_submissions.py", line 7, in <module>
r = requests.get(url)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\requests\sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\requests\sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Laeva\AppData\Roaming\Python\Python36\site-packages\requests\adapters.py", line 513, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='hacker-news.firebaseio.com', port=443): Max retries exceeded with url: /v0/topstories.json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000002257BBCA978>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))
,显示的意思大概是某个连接无响应,但是我的电脑网络并没有连接问题,请问有大神了解这是什么原因吗?有什么解决方法吗?非常感谢!