Python 3.7之使用web api

2023-01-31 01:01:04 python api 3.7

1.调用GitHub的公共api


#需要提前安装requests 包

import requests

url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:",r.status_code)

response_dict = r.JSON()
print(response_dict.keys())
print("Total repositories:",response_dict['total_count'])

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

#研究第一个GitHub仓库
repo_dict = repo_dicts[0]
print("\nkeys:",len(repo_dict))
for key in sorted(repo_dict.keys()):
    print(key)

print("\nSelected infORMation about first repository:")
print('Name:',repo_dict['name'])
print('Owner:',repo_dict['owner']['login'])

 

相关文章