python生成github中Js的按Stars排序报表

python学习实践,根据github的API获取某个语言的项目列表生成图表

通过python的requests模块,对github的https://api.github.com/search/repositories?q=language:javascript&sort=starts接口进行请求,来获取我们需要搜索的语言数据。如果需要获取其他语言的数据,只要修改API的javascript为想要查看的语言即可。

具体代码:

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

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

r = requests.get(url)
print('Status code: ', r.status_code)

response_dict = r.json()


print('Total repositories: ', response_dict["total_count"])

repo_dicts = response_dict['items']

print('Repositories returned:', len(repo_dicts))

# names, stars = [], []
names, plot_dicts = [], []

print('\n Selected information about first respository:')

for repo_dict in repo_dicts:
  names.append(repo_dict['name'])
  # stars.append(repo_dict['stargazers_count'])
  plot_dict = {
    'value': repo_dict['stargazers_count'],
    'label': repo_dict['description'],
    'xlink': repo_dict['html_url']
  }
  plot_dicts.append(plot_dict)


my_style = LS('#333366', base_style=LCS)

my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_y_guides = False

chart = pygal.Bar(my_config, show_legend=False)
chart.title = 'Most-Starred Python Projects onn GitHub'
chart.x_labels = names

chart.add('stars', plot_dicts)
chart.render_to_file('python_respos.svg')

通过requests.get(url)获取我们需要的数据,通过pygal.Bar创建一个图标,先列表一下my_config都代表什么意思。

  • pygal.Config(): 创建一个Pygal类Connfig的实例
  • x_label_rotation: 让标签绕x轴旋转度数
  • show_y_guides: 隐藏x轴上的水平线

方法add()接收一个字符串和一个列表,render_to_file()这里我让其生成一个svg文件,列表的效果图如下:


1534054272085.jpg
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,790评论 19 139
  • 再次提笔书写今年的高考作文没有想到竟然是三年后的事情,在看到高考作文题目之后,我竟然质疑三年的大学时光究竟...
    烛下的眷恋阅读 380评论 0 0
  • DD5512阅读 685评论 0 0
  • 作者林丞音在文章《和申被贪背后的无奈》中提到: 史书上说,和申外形俊朗,才华横溢,深得老师的喜爱,遂配老师爱女,多...
    子曰少怀阅读 846评论 4 7
  • 在二十一这个欢天喜地的世纪 当真很多事情都开始改变了吗 ? 我肯定会说 是真改变了 而且是发生了翻天覆地的变化 看...
    树郦阅读 268评论 2 2

友情链接更多精彩内容