17-1 其他语言 修改python_repos.py中的API调用,使其在生成的图表中显示其他语言最受欢迎的项目。请尝试语言JavaScript、Ruby、C、Java、Perl、Haskell和Go。
import requests
from plotly.graph_objs import Bar
from plotly import offline
lans = ['javascript','ruby','c','java','perl','haskell','go']
# 遍历lans,使每一种语言都有对应图表
for lan in lans:
# 执行API调用并存储响应
url = "https://api.github.com/search/repositories?q=language:"+lan+"&sort=stars"
headers = {
'Accept':'application/vnd.github.v3+json'}
r = requests.get(url,headers=headers)
print(f'Status code:{
r.status_code}')
# 处理结果
# 将API响应存储在一个变量中
response_dict = r.json()
# 仓库的信息
repo_dicts = response_dict['items']
repo_names,stars,labels = [],[],[]
for repo_dict in repo_dicts:
repo_names.append(repo_dict['name'])
stars.append(repo_dict['stargazers_count'])
owner = repo_dict['owner']['login']
description = repo_dict['description'</