使用Web應(yīng)用編程接口(API)自動(dòng)請(qǐng)求網(wǎng)站的特定信息而不是整個(gè)網(wǎng)頁(yè)迹恐,再對(duì)這些信息進(jìn)行可視化挣惰,web API是網(wǎng)站的一部分,用于與使用非常具體的URL請(qǐng)求特定信息的程序交互殴边。這種請(qǐng)求稱(chēng)為API調(diào)用憎茂。請(qǐng)求的數(shù)據(jù)將以易于處理的格式(如JSON或CSV)返回。依賴(lài)于外部數(shù)據(jù)源的大多數(shù)應(yīng)用程序都依賴(lài)于API 調(diào)用锤岸,如集成社交媒體網(wǎng)站的應(yīng)用程序唇辨。
參考網(wǎng)址 https://blog.csdn.net/weixin_40575956/article/details/80148472
import requests
# 執(zhí)行API調(diào)用并存儲(chǔ)響應(yīng)
url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
r = requests.get(url)
print("Status code:", r.status_code)
# 將API響應(yīng)存儲(chǔ)在一個(gè)變量中
# print(r.text)
# print(type(r.text))
response_dict = r.json()
# # 處理結(jié)果
print(response_dict.keys())
# print(type(response_dict))
print("Total repositories:", response_dict['total_count'])
# 探索有關(guān)倉(cāng)庫(kù)的信息
repo_dicts = response_dict['items']
# print(repo_dicts)
print("Repositories returned:", len(repo_dicts))
# 研究有關(guān)倉(cāng)庫(kù)的信息
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個(gè)倉(cāng)庫(kù)"%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