#-*- coding:utf-8 -*-
import requests
from operator import itemgetter
执行API调用并存储响应
url = 'http://hacker-news.firebaseio.com/v0/topstories.json'
r = requests.get(url)
print("Status code:", r.status_code)
处理有关每篇文章的信息
submission_ids = r.json()
submission_dicts = []
for submission_id in submission_ids[:30]:
# 对于每篇文章,都执行一个API调用
url = ('http://hacker-news.firebaseio.com/v0/item/' + str(submission_id) + '.json')
submission_r = requesets.get(url)
print(submisssion_r.status_code)
reponse_dict = submission_r.json()
submission_dict = {
'title': resopnse_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'), recerse=Ture)
for submission_dict in submission_dicts:
print("/nTitle:", submission_dict['title'])
print("Discussion link:", submission_dict['link'])
print("Comeents", submission_dict['comments'])