python 調用github的api,呈現python的受歡迎的程度

1 使用api調用數據:

 在瀏覽器的地址欄中輸入:html

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

 這個api調用github當前託管的python項目node

{
  "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項目python

"incomplete_results": true, 表示請求成功,若是github沒法處理該api他返回的值是truegit

"items"python項目的具體的信息github

2 安裝requests,

requests 能夠讓python程序輕鬆向網站請求信息以及檢查返回的響應json

pip install --user requestsapi

表明僅該用戶的安裝,安裝後僅該用戶可用。處於安全考慮,儘可能使用該命令進行安裝。瀏覽器

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
    },

簡單的倉庫的信息作介紹:具體的介紹看文檔app

響應的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中,將鼠標指向條形將顯示它表示的信息,這一般稱爲工具提示,建立工具欄,同時顯示項目的描述,獲取的星

相關文章
相關標籤/搜索