1、处理API响应
import requests url = 'https://api.github.com/search/repositories?q=language:python&sort=stars%27' r=requests.get(url) print('Status code:',r.status_code) response_dict = r.json() print(response_dict.keys()) repo_dicts = response_dict['items'] print("Repositories returned:", len(repo_dicts)) repo_dict = repo_dicts[0] print("\nKeys:",len(repo_dict)) for key in sorted(repo_dict.keys()): print(key)
2、可视化
import requests import pygal from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS #执行api并存储响应 url = 'https://api.github.com/search/repositories?q=language:python&sort=stars%27' r=requests.get(url) print('Status code:',r.status_code) #将api存储在一个变量中 response_dict = r.json() print("Total repositories:", response_dict['total_count']) # print(response_dict.keys()) repo_dicts = response_dict['items'] print("Repositories returned:", len(repo_dicts)) # repo_dict = repo_dicts[0] # print("\nKeys:",len(repo_dict)) # for key in sorted(repo_dict.keys()): # print(key) # for repo_dict in repo_dicts: # print("name: "+repo_dict['name']) names, stats = [], [] for repo_dict in repo_dicts: names.append(repo_dict['name']) stats.append(repo_dict['stargazers_count']) #可视化 my_style = LS('#333366',base_style=LCS) chart = pygal.Bar(style=my_style, x_label_rotation=45,show_ledend=False) chart.x_labels = names chart.add(" ",stats) chart.render_to_file('python_repos.svg')
3、修改图表
按照书中代码会出问题
解决参考:https://blog.csdn.net/HXZ_CREATE/article/details/108984427
4、Hacker News API
为探索如何使用其他网站的API调用,我们来看看HackerNews(http://news.ycombinator.com/ )。在Hacker News网站,用户分享 编程和技术方面的文章,并就这些文章展开积极的讨论。Hacker News的API让你能够访问有关该网站所有文章和评论的信息,且不要求你通过注册获得密钥。标签:dicts,repo,API,dict,使用,print,response From: https://www.cnblogs.com/buffaloes/p/16916250.html