import request #导入模块
from plotly.graph_objs import Bar
from plotly import offline
#执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' #存储API调用的url
headers = {'Accpet':'application/vnd/github.v3+json'} #API版本
r = requests.get(url,headers = headers) #request调用API,get()响应对象赋给r
print(f"Status code:{r.status_code}") #status_code指出请求是否成功,状态码200表示请求成功
添加自定义工具提示:
#处理结果
response_dict = r.json() #json转换为字典
#探索有关仓库的信息
repo_dicts = response_dict['items'] #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'] #提取每个项目中的描述
label = f"{owner}
{description}" #HTML代码 所有者-换行符-描述
labels.append(label) #添加到labels中
#可视化
data = [{
'type':'bar', #图标类型
'x':repo_names, #x值
'y':stars, #y值
'hovertext':labels, #自定义工具提示:悬停文本
'maker':{
'color':'rgb(60,100,150)', #蓝色
'line':{'width':1.5,'color':'rgb(25,25,25)'} #宽1.5像素的深灰色轮廓
},
'opacity':0.6, #不透明度0.6
}]
my_layout = { #设计布局
'title':'Github上最受欢迎的Python项目', #图标名称
'titlefont':{'size':28},
#x轴标签
'xaxis':{
'title':'Repository',
'titlefont':{'size':24},
'tickfont':{'size':14}, #刻度表字号
},
#y轴标签
'yaxis':{
'title':'Stars',
'titlefont':{'size':24},
'tickfont':{'size':14}, #刻度表字号
},
}
fig = {'data':data,'layout':my_layout}
offline.plot(fig,filename='python_repos.html')
在图标中添加可单击的链接:
#处理结果
response_dict = r.json() #json转换为字典
#探索有关仓库的信息
repo_dicts = response_dict['items'] #items关联值是列表,其中包含很多字典
repo_links,stars,labels = [],[],[] #创建三个空列表存储数据
#遍历所有字典
for repo_dict in repo_dicts:
repo_name = repo_dict['name'] #提取每个项目中的名字
repo_url = repo_dict['html_url'] #提取每个项目中的url
repo_link = f"{repo_name}" #linktext
repo_links.append(repo_link) #添加进去
stars.append(repo_dict['stargazers_count']) #提取每个项目中的星级,并添加
owner = repo_dict['owner']['login'] #提取每个项目中的所有者
description = repo_dict['description'] #提取每个项目中的描述
label = f"{owner}{description}" #HTML代码 所有者-换行符-描述
labels.append(label) #添加到labels中
#可视化
data = [{
'type':'bar', #图标类型
'x':repo_links,
'y':stars, #y值
'hovertext':labels, #自定义工具提示:悬停文本
'maker':{
'color':'rgb(60,100,150)', #蓝色
'line':{'width':1.5,'color':'rgb(25,25,25)'} #宽1.5像素的深灰色轮廓
},
'opacity':0.6, #不透明度0.6
}]
Hacker News API:
#调用返回本书编写期间最热门的文章的信息:
http://hacker-news.firebaseio.com/v0/item/19155826.json
import requests
import json
#执行API调用并存储响应
url = 'http://hacker-news.firebaseio.com/v0/item/19155826.json'
r = requests.get(url)
print(f"Status code:{r.status_code}")
#探索数据的结构
response_dict = r.json()
readable_file = 'data/readable_hn_data.json'
with open(readable_file,'w') as f:
json.dump(response_dict,f,indent = 4) #运行这个URL
#URL返回一个列表,其中包含Hacker News上排名靠前的文章的ID:
http://hacker-news.firebaseio.com/v0/topstories.json
#获取主页每篇文章
from operator import itemgetter
import requests
#执行API调用并存储
url = 'http://hacker-news.firebaseio.com/v0/topstories.json'
r = requests.get(url)
print(f"Status code:{r.status_code}")
#处理有关每篇文章的信息
submission_ids = r.json() #转换为列表并存储到ids中
submission_dicts = [] #创建空列表
for submission_id insubmission_ids[:30]:
#对于每篇文章,都执行一个API调用,URL包含submission_id的当前值
url = f"http://hacker-news.firebaseio.com/v0/item/{submission_id}.json"
r = requests.get(url)
print(f"id:{submission_id}\tstatus:{r.status_code}")
response_dict = r.json()
#对于每篇文章,都创建一个字典
submission_dict = {
'title':response_dict['title'], #存储文章标题
'hn_link':f"http://hacker-news.firebaseio.com/item?id={submission_id}", #存储页面链接
'comments':response_dict['descendants'], #存储评论数
}
submission_dicts.append(submission_dict)
#根据键comments关联的值,对列表进行排序,降序
submission_dicts = sorted(submission_dicts,key = itemgetter('comments'),reverse = True)
for submission_dict in submission_dicts:
print(f"\nTitle:{submission_dict['title']}")
print(f"Discussion link:{submission_dict['hn_link']}")
print(f"Comments:{submission_dict['comments']}")