使用Web应用编程接口(API)自动请求网站的特定信息而不是整个网页,再对这些信息进行可视化,web API是网站的一部分,用于与使用非常具体的URL请求特定信息的程序交互。这种请求称为API调用。请求的数据将以易于处理的格式(如JSON或CSV)返回。依赖于外部数据源的大多数应用程序都依赖于API 调用,如集成社交媒体网站的应用程序。
参考网址 https://blog.csdn.net/weixin_40575956/article/details/80148472
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响应存储在一个变量中
# print(r.text)
# print(type(r.text))
response_dict = r.json()
# # 处理结果
print(response_dict.keys())
# print(type(response_dict))
print("Total repositories:", response_dict['total_count'])
# 探索有关仓库的信息
repo_dicts = response_dict['items']
# print(repo_dicts)
print("Repositories returned:", len(repo_dicts))
# 研究有关仓库的信息
repo_dict = repo_dicts[0]
print('\nkeys:', len(repo_dict))
i = 1
for key in sorted(repo_dict.keys()):
print(i, key, sep=" : ")
i += 1
print("\nSelected information about first repository")
print("name:",repo_dict['name'])
print("owner:", repo_dict['owner']['login'])
print("start:", 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("\nSelected information about each repository")
k = 1
for repo_dict in repo_dicts:
print("\n 第%d个仓库"%k)
print("name:", repo_dict['name'])
print("owner:", repo_dict['owner']['login'])
print("start:", 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'])
k += 1
网友评论