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',))

,显示的意思大概是某个连接无响应,但是我的电脑网络并没有连接问题,请问有大神了解这是什么原因吗?有什么解决方法吗?非常感谢!
...全文
482 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Xue_Gu 2020-01-08
  • 打赏
  • 举报
回复
可以访问,需要配置代理来访问这个网站。在request里面用proxies
jinzhu1911 2019-03-05
  • 打赏
  • 举报
回复 1
浏览器这个网址都已经打不开了,估计已经停止服务了。
https://news.ycombinator.com/news这个网址倒还是存在
仙道的人字拖 2018-11-20
  • 打赏
  • 举报
回复
我也遇到这样的问题了,不知道是不是本来这个网址就已经打不开了的原因

37,717

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧