zoukankan      html  css  js  c++  java
  • python 调用github的api,呈现python的受欢迎的程度

    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中,将鼠标指向条形将显示它表示的信息,这通常称为工具提示,创建工具栏,同时显示项目的描述,获取的星

  • 相关阅读:
    解决原子性问题?脑海中有这个模型就可以了
    基础面试,为什么面试官总喜欢问String?
    学并发编程,透彻理解这三个核心是关键
    Spring Boot 统一异常这样处理和剖析,安否?
    每天用SpringBoot,还不懂RESTful API返回统一数据格式是怎么实现的?
    Java网络编程的Java流介绍
    Java设计模式学习记录-模板方法模式
    Java网络编程的基本网络概念
    Java设计模式学习记录-状态模式
    Java设计模式学习记录-观察者模式
  • 原文地址:https://www.cnblogs.com/lulin9501/p/11147199.html
Copyright © 2011-2022 走看看