python入门-使用API

import requests

#执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)

print('status code', r.status_code)

#把api响应存储在一个变量中
response_dict = r.json()
print("Total repositories:", response_dict['total_count'])

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

#研究第一个仓库
repo_dict = repo_dicts[0]
print('\nSelected information about first repository:')
print("Name", repo_dict['name'])
print("Ower", repo_dict['owner']['login'])
print("Stars", repo_dict['stargazers_count'])
print("Repository:", repo_dict['html_url'])
print("Created:", repo_dict['created_at'])
print("Updated:", repo_dict['updated_at'])
print("Description:", repo_dict['description'])
#print("\nKeys:", len(repo_dict))

#for key in sorted(repo_dict.keys()):
    #print(key)
#处理结果
#print(response_dict.keys())

如果之前没有requests记得安装下

pip install --user requests

 

相关文章:

  • 2022-12-23
  • 2021-09-20
  • 2021-08-17
  • 2021-11-30
  • 2021-09-20
  • 2019-07-16
  • 2022-02-02
  • 2021-09-22
猜你喜欢
  • 2021-09-02
  • 2021-11-14
  • 2021-07-19
  • 2022-12-23
  • 2021-10-15
  • 2021-05-22
  • 2022-02-25
相关资源
相似解决方案