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让你能够访问有关该网站所有文章和评论的信息,且不要求你通过注册获得密钥。
原文地址:http://www.cnblogs.com/buffaloes/p/16916250.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性