目前學到了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',))
,顯示的意思大概是某個連接無回應,但是我的電腦網路并沒有連接問題,請問有大神了解這是什么原因嗎?有什么解決方法嗎?非常感謝!
uj5u.com熱心網友回復:
我也遇到這樣的問題了,不知道是不是本來這個網址就已經打不開了的原因uj5u.com熱心網友回復:
瀏覽器這個網址都已經打不開了,估計已經停止服務了。https://news.ycombinator.com/news這個網址倒還是存在
uj5u.com熱心網友回復:
可以訪問,需要配置代理來訪問這個網站。在request里面用proxies轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/105053.html
