您的位置:首页 > 编程语言 > Python开发

Python搜集Github中项目作者信息

2018-03-27 23:14 309 查看
我们将继续学习可视化部分的内容,我们将请求网站信息,并对这些信息进行可视化操作
很多API都要求注册获得API秘钥后才能执行API调用,但是Github没有这样的要求 https://api/github.com/将请求发送到GitHub网站中相应API调用的部分,search/repositories让API搜索 Github上的所有仓库 后面是传递的一个实参 q表示查询,通过language:python指出只想获取主要语言为Python的仓库信息
&sort=stars 指定将项目按其获得的星级进行排序

下面是代码部分:import requests
import sys

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

r = requests.get(url)
print("Status Code: ", r.status_code)

# 将API响应存储到一个变量中
response_dict = r.json()

#print(response_dict.keys())
print("Toal repositories:", response_dict['total_count'])

# 查找有关仓库的信息
repo_dicts = response_dict['items']
print("Repositories returned:", len(repo_dicts))

# 打印出所有仓库的项目的一些信息
print("\nSelected information about first repository:")

def getRepositoryInfomation():

# 作者名
print('\nName:', repo_dict['name'])
# 登录名
print('Owner:', repo_dict['owner']['login'])
# 星星个数
print('Stars:', repo_dict['stargazers_count'])
# 仓库的url
print('Repository:', repo_dict['html_url'])
# 创建时间
print('Created:', repo_dict['created_at'])
# 更新时间
print('Updated:', repo_dict['updated_at'])
# 可能会出现这种情况,"description": null这样会返回一个NoneType异常
if type(repo_dict['description']):
print('Description: ', 'null')
else:

# 项目描述
print('Description:', repo_dict['description'])

for repo_dict in repo_dicts:
getRepositoryInfomation()

'''
Status Code: 200
Toal repositories: 2484834
Repositories returned: 30

Selected information about first repository:

Name: awesome-python
Owner: vinta
Stars: 47594
Repository: https://github.com/vinta/awesome-python Created: 2014-06-27T21:00:06Z
Updated: 2018-03-27T14:45:05Z
Description: null

Name: youtube-dl
Owner: rg3
Stars: 35318
Repository: https://github.com/rg3/youtube-dl Created: 2010-10-31T14:35:07Z
Updated: 2018-03-27T15:03:11Z
Description: null

Name: public-apis
Owner: toddmotto
Stars: 34801
Repository: https://github.com/toddmotto/public-apis Created: 2016-03-20T23:49:42Z
Updated: 2018-03-27T14:34:05Z
Description: null

Name: httpie
Owner: jakubroztocil
Stars: 34626
Repository: https://github.com/jakubroztocil/httpie Created: 2012-02-25T12:39:13Z
Updated: 2018-03-27T14:15:34Z
Description: null
snip.....
'''你可以访问点击打开链接 请求API速率有一定的要求,所以一些爬虫就会通过很多个代理Ip进行爬取信息,这里我们可以访问这个链接查看自己的速率限制
下一节我们将使用Pygal可视化这些数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  可视化 Python Pygal