1 使用api调用数据:

在浏览器的地址栏中输入:

https://api.github.com/search/repositories?q=language:python&sort=starts

这个api调用github当前托管的python项目

{
"total_count": 3872430,
"incomplete_results": true,
"items": [
{
"id": 21289110,
"node_id": "MDEwOlJlcG9zaXRvcnkyMTI4OTExMA==",
"name": "awesome-python",
"full_name": "vinta/awesome-python",
"private": false,
---snip----

"total_count":  github上共有的python项目

"incomplete_results": true, 表示请求成功,如果github无法处理该api他返回的值是true

"items"python项目的具体的信息

2 安装requests,

requests 可以让python程序轻松向网站请求信息以及检查返回的响应

pip install --user requests

代表仅该用户的安装,安装后仅该用户可用。处于安全考虑,尽量使用该命令进行安装。

3 简单的处理api的响应

import requests
url = 'https://api.github.com/search/repositories?q=language:python&sort=starts'
r = requests.get(url) # 打印出状态码
print('status code',r.status_code)
# status code 200 转态码 200表示成功 response_dict = r.json() # 打印出所有的keys
print(response_dict.keys())
# dict_keys(['total_count', 'incomplete_results', 'items'])

4仓库的信息

仓库的信息:

{
"id": 21289110,
"node_id": "MDEwOlJlcG9zaXRvcnkyMTI4OTExMA==",
"name": "awesome-python",
"full_name": "vinta/awesome-python",
"private": false,
"owner": {
"login": "vinta",
"id": 652070,
"node_id": "MDQ6VXNlcjY1MjA3MA==",
"avatar_url": "https://avatars2.githubusercontent.com/u/652070?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/vinta",
"html_url": "https://github.com/vinta",
"followers_url": "https://api.github.com/users/vinta/followers",
"following_url": "https://api.github.com/users/vinta/following{/other_user}",
"gists_url": "https://api.github.com/users/vinta/gists{/gist_id}",
"starred_url": "https://api.github.com/users/vinta/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/vinta/subscriptions",
"organizations_url": "https://api.github.com/users/vinta/orgs",
"repos_url": "https://api.github.com/users/vinta/repos",
"events_url": "https://api.github.com/users/vinta/events{/privacy}",
"received_events_url": "https://api.github.com/users/vinta/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/vinta/awesome-python",
"description": "A curated list of awesome Python frameworks, libraries, software and resources",
"fork": false,
"url": "https://api.github.com/repos/vinta/awesome-python",
"forks_url": "https://api.github.com/repos/vinta/awesome-python/forks",
"keys_url": "https://api.github.com/repos/vinta/awesome-python/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/vinta/awesome-python/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/vinta/awesome-python/teams",
"hooks_url": "https://api.github.com/repos/vinta/awesome-python/hooks",
"issue_events_url": "https://api.github.com/repos/vinta/awesome-python/issues/events{/number}",
"events_url": "https://api.github.com/repos/vinta/awesome-python/events",
"assignees_url": "https://api.github.com/repos/vinta/awesome-python/assignees{/user}",
"branches_url": "https://api.github.com/repos/vinta/awesome-python/branches{/branch}",
"tags_url": "https://api.github.com/repos/vinta/awesome-python/tags",
"blobs_url": "https://api.github.com/repos/vinta/awesome-python/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/vinta/awesome-python/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/vinta/awesome-python/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/vinta/awesome-python/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/vinta/awesome-python/statuses/{sha}",
"languages_url": "https://api.github.com/repos/vinta/awesome-python/languages",
"stargazers_url": "https://api.github.com/repos/vinta/awesome-python/stargazers",
"contributors_url": "https://api.github.com/repos/vinta/awesome-python/contributors",
"subscribers_url": "https://api.github.com/repos/vinta/awesome-python/subscribers",
"subscription_url": "https://api.github.com/repos/vinta/awesome-python/subscription",
"commits_url": "https://api.github.com/repos/vinta/awesome-python/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/vinta/awesome-python/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/vinta/awesome-python/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/vinta/awesome-python/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/vinta/awesome-python/contents/{+path}",
"compare_url": "https://api.github.com/repos/vinta/awesome-python/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/vinta/awesome-python/merges",
"archive_url": "https://api.github.com/repos/vinta/awesome-python/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/vinta/awesome-python/downloads",
"issues_url": "https://api.github.com/repos/vinta/awesome-python/issues{/number}",
"pulls_url": "https://api.github.com/repos/vinta/awesome-python/pulls{/number}",
"milestones_url": "https://api.github.com/repos/vinta/awesome-python/milestones{/number}",
"notifications_url": "https://api.github.com/repos/vinta/awesome-python/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/vinta/awesome-python/labels{/name}",
"releases_url": "https://api.github.com/repos/vinta/awesome-python/releases{/id}",
"deployments_url": "https://api.github.com/repos/vinta/awesome-python/deployments",
"created_at": "2014-06-27T21:00:06Z",
"updated_at": "2019-07-07T08:12:00Z",
"pushed_at": "2019-07-05T11:37:09Z",
"git_url": "git://github.com/vinta/awesome-python.git",
"ssh_url": "git@github.com:vinta/awesome-python.git",
"clone_url": "https://github.com/vinta/awesome-python.git",
"svn_url": "https://github.com/vinta/awesome-python",
"homepage": "https://awesome-python.com/",
"size": 4914,
"stargazers_count": 69602,
"watchers_count": 69602,
"language": "Python",
"has_issues": true,
"has_projects": false,
"has_downloads": true,
"has_wiki": false,
"has_pages": true,
"forks_count": 13570,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 464,
"license": {
"key": "other",
"name": "Other",
"spdx_id": "NOASSERTION",
"url": null,
"node_id": "MDc6TGljZW5zZTA="
},
"forks": 13570,
"open_issues": 464,
"watchers": 69602,
"default_branch": "master",
"score": 1.0
},

简单的仓库的信息做介绍:具体的介绍看文档

响应的api已经按照stargazers_count进行排序了,第一个仓库就是星最高的

name  项目的名称

owner项目的拥有者的信息

stargazers_count 项目获取的星的个数

repos_url 项目的url

created_at 项目创建的时间

updated_at 项目更新的时间

5    打印仓库的信息(项目名称,项目的拥有者,项目的星数,项目的描述)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author : linlin
# @Software: PyCharm import requests
url = 'https://api.github.com/search/repositories?q=language:python&sort=starts'
r = requests.get(url) # 打印出状态码
print('status code',r.status_code)
# status code 200 转态码 200表示成功 response_dict = r.json() # 打印出所有的keys
print(response_dict.keys())
# dict_keys(['total_count', 'incomplete_results', 'items']) #共有多少个python的项目
print(response_dict['total_count']) # 有关仓库的信息
repo_dicts = response_dict['items']
print('Repositors returned',len(repo_dicts)) for repo_dict in repo_dicts:
# 项目的名称
print(repo_dict['name'])
# 项目的拥有者
print(repo_dict['owner']['login'])
# 项目的星数
print(repo_dict['stargazers_count'])
# 项目的url
print(repo_dict['html_url'])
# 项目的描述
print(repo_dict['description'])

6 监视api的速率限制

大多数的api都存在api速率限制,在特定的时间内可以执行的请求数存在限制,,看github的限制,请在浏览器上输入https://api.github.com/rate_limit

{
"resources": {
"core": {
"limit": 60,
"remaining": 60,
"reset": 1562495790
},
"search": {
"limit": 10,
"remaining": 10,
"reset": 1562492250
},
"graphql": {
"limit": 0,
"remaining": 0,
"reset": 1562495790
},
"integration_manifest": {
"limit": 5000,
"remaining": 5000,
"reset": 1562495790
}
},
"rate": {
"limit": 60,
"remaining": 60,
"reset": 1562495790
}
}

search 的解释:

"search": { "limit": 10, 每分钟10个请求

"remaining": 10, 在当前一分钟还可以请求10次

"reset": 1562492250 }, 配额将重置unix时间或新世纪时间(1970年1月1日午夜多少秒),当配额用完,您将收到一条简单的响应,有此可知,已达到api的极限,到达极限等待配额重置

配额用完访问https://api.github.com/search/repositories?q=language:python&sort=starts     status code 403状态码是403

7 使用pygal可视化仓库

使用图形将项目的受欢迎的程度表示出来

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS url = 'https://api.github.com/search/repositories?q=language:python&sort=starts'
r = requests.get(url) # 打印出状态码
print('status code',r.status_code)
# status code 200 转态码 200表示成功 response_dict = r.json() # 有关仓库的信息
repo_dicts = response_dict['items'] names,starts = [],[]
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
starts.append(repo_dict['stargazers_count']) # 可视化 # 定义图形的格式 333366 蓝色的
my_style = LS('#333366',base_style=LCS)
# x_lable_rotation 标签倾斜45度 show_legend=False 隐藏图例
chart = pygal.Bar(style=my_style,x_lable_rotation=45,show_legend=False) # 图标的标题
chart.title= 'Most-Stsrred Python Projects on Github'
chart.x_labels = names
chart.add('',starts)
chart.render_to_file('python_repos.svg')

运行项目会生成python_repos.svg 文件用浏览器打开:

8 改进pygal图标

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author : linlin
# @Software: PyCharm import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS url = 'https://api.github.com/search/repositories?q=language:python&sort=starts'
r = requests.get(url) # 打印出状态码
print('status code',r.status_code)
# status code 200 转态码 200表示成功 response_dict = r.json() # 有关仓库的信息
repo_dicts = response_dict['items'] names,starts = [],[]
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
starts.append(repo_dict['stargazers_count']) # 可视化 # 定义图形的格式 333366 蓝色的
my_style = LS('#333366',base_style=LCS) my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False # 图标标题,副标题.,主标签的字体的大小
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_lable_font_size = 18 # 较长的项目名缩短为15个字符,鼠标移动到被截断的项目名,会显示完整的项目名
my_config.truncate_label = 15
# 隐藏图标的水平线
my_config.show_y_guides = False
# 自动定义高度
my_config.width = 1000 # x_lable_rotation 标签倾斜45度 show_legend=False 隐藏图例
chart = pygal.Bar(my_config,style=my_style) # 图标的标题
chart.title= 'Most-Stsrred Python Projects on Github'
chart.x_labels = names
chart.add('',starts)
chart.render_to_file('python_repos.svg')

9 添加自定义工具提示

在pygal中,将鼠标指向条形将显示它表示的信息,这通常称为工具提示,创建工具栏,同时显示项目的描述,获取的星

python 调用github的api,呈现python的受欢迎的程度的更多相关文章

  1. python调用openstack的api,create_instance的程序解析

    python调用openstack的api,create_instance的程序解析 2017年10月17日 15:27:24 CloudXli 阅读数:848   版权声明:本文为博主原创文章,未经 ...

  2. Python 调用图像融合API

    Python 调用图像融合API 本文记录使用Python,调用腾讯AI开放平台的图像融合API.官网给出的Demo用的是PHP,博主作为Python的粉丝,自然想用它来和『最好的』的语言一较高下,顺 ...

  3. 用Python调用华为云API接口发短信

    [摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...

  4. Python 调用百度翻译API

    由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...

  5. Python 调用阿里云 API 收集 ECS 数据

    #!/usr/bin/env python # coding: utf-8 # author: Wang XiaoQiang ''' 功能介绍: 1.调用阿里云API,收集所有区域 ECS 信息 2. ...

  6. Python调用百度地图API实现批量经纬度转换为实际省市地点(api调用,json解析,excel读取与写入)

    1.获取秘钥 调用百度地图API实现得申请百度账号或者登陆百度账号,然后申请自己的ak秘钥.链接如下:http://lbsyun.baidu.com/apiconsole/key?applicatio ...

  7. PYTHON调用JENKINS的API来进行CI

    我查到的相关API有两套,我主要用的是python-jenkins. https://pypi.python.org/pypi/python-jenkins/ 按语法调用即可... import je ...

  8. python调用ansible接口API执行命令

    python版本:Python 2.6.6 ansible版本:ansible 2.3.1.0      下载地址:https://releases.ansible.com/ansible/ 调用脚本 ...

  9. Python 调用图灵机器人 API

    ''' Python3''' import requests #导入requests库 import json #导入json库 key = '3119f1e3610f42c5977ea73c4097 ...

随机推荐

  1. iOS 9.0中UIAlertController的用法。

    1.我为什么要写这篇博客记录它? 答:因为 UIAlertView和UIActionSheet 被划线了 苹果不推荐我们使用这两个类了,也不再进行维护和更新,为了以后方便使用我来记录一下.如图所示 正 ...

  2. 如何将Chrome本地安装的扩展应用导出到本地

    有时由于种种原因,我们不能直接使用Chrome web store进行Chrome扩展应用的安装,这时可以让一位已经安装了某Chrome扩展应用的朋友将他的应用导出到本地成为.crx文件,然后发给你, ...

  3. 7.Redis的发布订阅

    Redis消息的订阅/发布 a)是什么 进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. b)命令 SUBSCRIBE订阅 PUBLISH发布 c)案例 先订阅后发布后才 ...

  4. XML文件解析之JDOM解析

    1.JDOM介绍 JDOM的官方网站是http://www.jdom.org/,JDOM解析用到的jar包可以在http://www.jdom.org/dist/binary/中下载,最新的JDOM2 ...

  5. MyBatis工厂工具类 MyBatisUtils

    import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apach ...

  6. 初识cv

    验证码识别的一般套路: 灰度化.图像处理.二值化.选算法.训练.评估调整参数.预测,当然,我在这里二值化与处理的顺序换了一下,灵活处理哈 1 显示图片轮廓 img = cv2.Canny(img,25 ...

  7. 深度学习_1_神经网络_4_分布式Tensorflow

    分布式Tensorflow 单机多卡(gpu) 多级多卡(分布式) 自实现分布式 API: ​ 1,创建一个tf.train.ClusterSpec,用于对集群的所有任务进行描述,该描述对于所有任务相 ...

  8. init system

    参考:5 Best Modern Linux ‘init’ Systems (1992-2015) 参考:Linux开机流程 参考:<鸟哥的 Linux 私房菜:基础学习篇 第四版>第十七 ...

  9. 【2017-12-06】winfrom 窗体自启最大化,控件自适应

    先将窗体windowstate属性设置为Maximized public partial class Form1 : Form { public Form1() { InitializeCompone ...

  10. springmvc,hibernate整合时候出现Cannot load JDBC driver class 'com.mysql.jdbc.Driver

    原因:不清楚是什么原因,哪位知道可以给我留言,不胜感激! 解决方法: 1.把mysql的驱动包放到你项目的WEB-INF目录下的lib目录中2.要mysql的驱动包放在tomcat/lib目录下