import requests
from plotly.graph_objs import Bar
from plotly import offline
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)
response_dict = r.json()
# print(response_dict.keys()) 整个文档里大的key
# print(response_dict['total_count']) 指出GitHub总共包含多少仓库
repo_dicts = response_dict['items']
# print(len(repo_dicts))
# repo_dict = repo_dicts[0] # 取第一个仓库
# print(repo_dict.keys())
# for key in sorted(repo_dict):
# print(key)
# print("\nSelected information about each repository")
# for repo_dict in repo_dicts:
# print(f"\nName:{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']}")
#repo_names = []
stars = []
labels = []
repo_links=[]
for repo_dict in repo_dicts:
#repo_names.append(repo_dict['name'])
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'])
# print(stars[:10])
# data=[Bar(x=repo_names,y=stars)]
owner = repo_dict['owner']['login']
description = repo_dict['description']
label = f"{owner}<br/>{description}"
labels.append(label)
data = [{
'type': 'bar',
# 'x': repo_names,
'x':repo_links,
'y': stars,
'hovertext': labels, #鼠标靠近显示
'marker': {
'color': 'rgb(60,100,150)',
'line': {'width': 2, 'color': 'red'}, # 边框
'opacity': 0.8 # 透明度
}
}]
my_layout = {
'title': 'Github最受欢迎项目',
'xaxis': {
'title': 'Repository',
'titlefont': {'size': 44}, # 标签
'tickfont': {'size': 14} # 刻度
},
'yaxis': {'title': 'Stars'},
}
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='66.html')
Github requests plotly
最新推荐文章于 2024-07-22 16:53:38 发布