600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 用Github的Api发现stars最多开源项目

用Github的Api发现stars最多开源项目

时间:2021-11-15 17:25:29

相关推荐

用Github的Api发现stars最多开源项目

Github的API无需注册,在网上可以查到使用信息和一些限制:

查询的请求为每分钟10次

此次用到的api格式如下:

url = ‘/search/repositories?q=language:python&sort=stars’

/search/repositories 搜索github上的仓库

?传递一个查询参数q,=指定查询的内容language:python,查询语言为python的仓库信息

&sort=stars项目按照获得的星星数量排名

倒入用到的库

import requests

# 获取最受欢迎的前30个仓库的信息,返回对象为一个字典对象def get_info(language):url = '/search/repositories?q=language:%s&sort=stars'%(language)r = requests.get(url)if r.status_code == 200:print 'success'return r.json()

# 获取python,java,C++三种语言最受欢迎的库response_python = get_info('python')response_java = get_info('java')response_c_dplus = get_info('C++')

successsuccesssuccess

获取信息函数返回字典对象的键值

print (response_python.keys())

[u’total_count’, u’items’, u’incomplete_results’]

response_java['total_count'] # github上java语言仓库的数量

3453199

response_python['items'][0].keys() # item包含仓库的一些详细信息的列表

[u'issues_url',u'deployments_url',u'stargazers_count',u'forks_url',u'mirror_url',u'subscription_url',u'notifications_url',u'collaborators_url',........u'watchers',u'name',u'language',u'url',u'created_at',u'pushed_at',u'forks_count',u'default_branch',u'teams_url',u'trees_url',u'branches_url',u'subscribers_url',u'stargazers_url']

incomplete_results,在执行更复杂的Api调用是,需要检查它的结果.简单的调用可以忽略

对字典对象进行处理,生成一个DataFrame对象

# 返回一个dataframe对象import pandas as pddef ret_df(response_dict):df = pd.DataFrame(columns=['created_at','updated_at','name','forks' ,'stars','size'])for resp_dict in response_dict['items']:df = df.append({'created_at':resp_dict['created_at'],'updated_at':resp_dict['updated_at'],'name':resp_dict['name'],'forks':resp_dict['forks'],'stars':resp_dict['stargazers_count'],'size':resp_dict['size']},ignore_index=True)return df

Python语言前5个最受欢迎的仓库

可视化仓库

# pygal对仓库信息进行可视化import pygal

# 生成数据df_python = ret_df(response_python)df_java = ret_df(response_java)df_c_dplus = ret_df(response_c_dplus)

每个仓库的stars和forks

def show(df, language):line_chart = pygal.Line(x_label_rotation=45)line_chart.title = 'Most-Starred %s projects on Github'%languageline_chart.x_labels = df['name']line_chart.add('forks',df['forks'])line_chart.add('stars',df['stars'])line_chart.render_to_file(language+'_projects.svg')

show(df_c_dplus, 'C++')

不同语言的比较

# 不同语言比较from pygal.style import TurquoiseStyledef compare_show(df_py,df_j,df_c, colu):chart = pygal.Bar()chart.title = 'Compare different language'chart.x_labels = range(1,31) #项目星星排名chart.add('Python', df_py[colu])chart.add('Java', df_j[colu])chart.add('C++', df_c[colu])chart.render_to_file(colu+'.svg')

compare_show(df_python, df_java, df_c_dplus, 'stars')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。