结果如图:
这是18-03时候的排名
这是19-07时候的排名,可以看到,1年多过去了,变化还是不少的。
鼠标移上去会显示信息和链接
代码如下:
#根据星星数量对github上python项目依排名显示
#从requests改为当前版本的urllib.request,原始代码操作的json,这里将html转为json即可
import urllib.request as request
import json
import pygal
from pygal.style import LightColorizedStyle as lcs,LightenStyle as ls
#获取项目描述
def description(repo_dict):
des=repo_dict['description']
if des:
return des
else:
return ''
#地址,关键词python+stars
url='https://api.github.com/search/repositories?q=language:python&sort=stars'
file=request.urlopen(url)
html=file.read()
#html转json
response_dict=json.loads(html.decode('utf-8'))
file.close()
#print(response_dict['total_count'])
repo_dicts=response_dict['items']
names,plot_dicts=[],[]
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
#项目描述无法显示,改了部分代码,目前版本可以显示项目描述了
#3部分内容:星号数量、项目描述、项目链接
plot_dict={'value':repo_dict['stargazers_count'],'label':description(repo_dict),'xlink':repo_dict['html_url'],}
plot_dicts.append(plot_dict)
my_style=ls('#336699',base_style=lcs)
#开始绘图,设置基本参数
my_config=pygal.Config()
my_config.x_label_rotation=-45 #标签旋转-45度,文字方向左下至右上
my_config.show_legend=False #不在右上角等地方再显示每个项目的信息了
my_config.title_font_size=24
my_config.label_font_size=14
my_config.major_label_font_size=18
my_config.truncate_label=15 #将名称缩写为15个字符,鼠标指向时显示全名
my_config.show_y_guides=False #隐藏图表中水平线
my_config.width=1000
#条形图
chart=pygal.Bar(my_config,style=my_style)#类似x_label_rotation等属性也可在这里给定
chart.title='most-starred python projects on github'
chart.x_labels=names#指定x列表
chart.add('',plot_dicts)#指定y列表
chart.render_to_file('python_repos_1.svg')#存储,使用浏览器打开