首页 > 其他分享 >使用API

使用API

时间:2022-11-24 11:14:37浏览次数:38  
标签:dicts repo API dict 使用 print response

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

相关文章

  • 【Amadeus原创】Centos使用图形化界面配置网络
    1.查看当前ip地址#ipaddr2.图形化界面配置网卡#nmtui界面提示,左右上下配置,OK即可。......
  • ORCAD使用问题总结
    1、新添加元件,复制元件时自动编号处理:Options-Preference,如图3-175所示,来进行设置参数; 图3-175参数设置示意图 第三步,执行上述命令以后,会弹出如图3-176所示的界面,......
  • react-app-rewired的使用备注&&禁止生成.map(对应隐藏源码)
    一:使用react-app-rewired时,除了根据文档对应修改设置外,还给予了一些配合此插件直接使用的webpack插件集:​​https://github.com/timarney/react-app-rewired#version-1x......
  • 使用sphinx-doc优雅的书写html和项目介绍,包含restructureText常用语法
    ​​跳转到我的gitee直接下载测试项目​​​​​sphinx概述​​​​使用nginx配置静态页面展示sphinx-doc点击跳转​​系统:win10中WSL(Ubuntu18.04)编辑器:VScode插件:......
  • System.getProperty()_基本使用
      System.out.println("java版本号:"+System.getProperty("java.version"));//java版本号System.out.println("Java提供商名称:"+System.getProperty(......
  • ONVIF工具使用说明
       ONVIF工具使用说明作为视频安防领域的工作者,少不了和摄像头打交道,一般大部分厂商都支持ONVIF协议,所以还是有必要了解一下怎么使用ONVIF的官方工具。1、下载ONVI......
  • abap-使用vim做abap的编辑器
    abap-使用vim做为abap的外部编辑器sapgui7带的abap编辑器很不错了,但是我最近都是在用ubuntu系统,用的是forjava的gui,编辑器超难用。于是就想能否用自已习惯的编辑器来......
  • Redis管理平台Cachecloud使用LDAP进行认证登录
    cachecloud版本:https://github.com/sohutv/cachecloud/archive/refs/tags/2.2.tar.gz 公司使用ldap的ssl认证,java本人一点都不懂,耗费几天时间没有搞定,只能曲线救国,使用s......
  • origin 使用闪退问题
    它好像就是经常闪退,所以要随时保存,可以更改软件自动保存的时间间隔。点击tools选项,选择option,点击open/close选项,将自动保存的时间从12分钟改为1分钟。另外,当打开的工程......
  • 微服务之关于spring cloud 的创建使用以及远程调用
      注册中心:将所有需要用到的微服务注册到一起统一管理配置中心:将所有需要用到的微服务配置拉到一起进行配置管理服务集群:各个微服务之间的相互调用形成统一集群 ......