1. 程式人生 > >Python蒐集Github中專案作者資訊

Python蒐集Github中專案作者資訊

我們將繼續學習視覺化部分的內容,我們將請求網站資訊,並對這些資訊進行視覺化操作

很多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視覺化這些資料