API入門專案專案收集GitHub上熱門專案的資訊
API是網站的一部分,常用在如果我們想要網站上的一些資訊的時候,我們可以呼叫API請求資料再對這些資料可視化,并且這個資料還是實時的,大大提高了資料的可用性,
這次我們就用API呼叫GitHub上星級最高的python專案資訊,并使用plotly生成互動式的可視化圖表,
import requests#匯入request模塊
# 呼叫API并儲存回傳的回應
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}#因為版本往往不一樣,我們指定使用這個我們指定的API
r = requests.get(url, headers=headers)#用函式呼叫API
print(f"Status code: {r.status_code}")
#將回應賦給response_dict
response_dict= r.json()
#API回傳的Json資訊儲存在response_dict
print (response_dict.keys())
#列印出來看看
https://api.github.com/search/repositories
關于這個地址,開頭的https://api.github.com/是把請求發送到GitHub網站,接下里search是搜索,物件是所有的倉庫repositories,q表示查詢,=表示開始指定查詢language:python是值要獲取語言為python的資訊,最后&sort=stars指定將專案按星排序
列印出來后是這樣子的:狀態碼為200,回應字典只有三個鍵:[‘total_count’, ‘incomplete_results’, ‘items’]
Status code: 200
dict_keys([‘total_count’, ‘incomplete_results’, ‘items’])
import requests
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
response_dict= r.json()
print(f"Total repositories: {response_dict['total_count']}")
# 探索全部倉庫的資訊
repo_dicts = response_dict['items']#列印與total_count相關的值,它指出了GitHub共有多少個倉庫
print(f"Repositories returned: {len(repo_dicts)}")#將字典儲存在repo_dicts
#我們可以來看下第一個倉庫
repo_dict=repo_dicts[0]
print(f"\nKeys:{len(repo_dict)}")
for key in sorted(repo_dict.keys()):
print(key)
#我們來提取一些repo_dict中于一些鍵相關聯的值
這個是列印出來的值,我們可以從這里了解到實際出來的資料
Status code: 200
Total repositories: 6622696
Repositories returned: 30
Keys:74
archive_url
archived
assignees_url
太多了,跳過一部分
watchers
watchers_count
現在我們來提取一些repo_dict中于一些鍵相關聯的值
import requests
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
response_dict= r.json()
print(f"Total repositories: {response_dict['total_count']}")
repo_dicts = response_dict['items']
print(f"Repositories returned: {len(repo_dicts)}")
repo_dict=repo_dicts[0]
#我們來提取一些repo_dict中于一些鍵相關聯的值
print("\nSelected information about each repository:")
print(f"Name: {repo_dict['name']}")#人名
print(f"Owner: {repo_dict['owner']['login']}")
print(f"Stars: {repo_dict['stargazers_count']}")#獲得了多少個星
print(f"Repository: {repo_dict['html_url']}")
print(f"Created: {repo_dict['created_at']}")#專案創建的時間
print(f"Updated: {repo_dict['updated_at']}")#最后一次更新的時間
print(f"Description: {repo_dict['description']}")
結果就就是下面這樣子:
Status code: 200
Total repositories: 6618376
Repositories returned: 30
Selected information about each repository:
Name: system-design-primer
Owner: donnemartin
Stars: 119890
Repository: https://github.com/donnemartin/system-design-primer
Created: 2017-02-26T16:15:28Z
Updated: 2021-01-31T02:19:49Z
Description: Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.
Process finished with exit code 0
理清了資料后,那么我們就可以總體開始了
import requests
from plotly.graph_objs import Bar#匯入bar類
from plotly import offline#匯入offline模塊
# Make an API call and store the response.
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
# 處理結果.
response_dict = r.json()
repo_dicts = response_dict['items']
repo_links, stars, labels = [], [], []#創建三個空串列用來存盤我們要用的資料
for repo_dict in repo_dicts:#遍歷repo_dicts中的所有的字典,列印專案的名稱、所有者、星級等資訊,
repo_name = repo_dict['name']
repo_url = repo_dict['html_url']
repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)
stars.append(repo_dict['stargazers_count'])
owner = repo_dict['owner']['login']
description = repo_dict['description']
label = f"{owner}<br />{description}"
labels.append(label)
#開始可視化,定義串列data
data = [{
'type': 'bar',
'x': repo_links,
'y': stars,
'hovertext': labels,
'marker': {
'color': 'rgb(60, 100, 150)',
'line': {'width': 1.5, 'color': 'rgb(25, 25, 25)'}
},
'opacity': 0.6,
}]
#使用字典定義表格的布局
my_layout = {
'title': 'Most-Starred Python Projects on GitHub',
'titlefont': {'size': 28},
'xaxis': {
'title': 'Repository',
'titlefont': {'size': 24},
'tickfont': {'size': 14},
},
'yaxis': {
'title': 'Stars',
'titlefont': {'size': 24},
'tickfont': {'size': 14},
},
}
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='python_repos.html')
最后我們可以生成一個可視化的html檔案在瀏覽器打開

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255219.html
標籤:python
