API

1使⽤ Plotly 可视化仓库

2Hacker News API

使⽤ API 调⽤请求数据

https://api.github.com/search/repositories?q=language:python+sort:stars

安装requests

python -m pip install --user requests

使⽤ Plotly 可视化仓库

创建代码文件

import requests

# 执⾏ API 调⽤并查看响应
url = "https://api.github.com/search/repositories"   # 地址
url += "?q=language:python+sort:stars+stars:>10000"  # 将地址与查询参数连接到一起

headers = {"Accept": "application/vnd.github.v3+json"}  # 请求头:想要接收数据类型
r = requests.get(url, headers=headers)  # 向服务器发送获取数据请求
print(f"Status code: {r.status_code}")
# status_code状态码为r正确,为4或5请求有问题,4请求问题出在客户端(请求代码可能有问题...),5请求问题在服务器(服务器出现问题可能在维修...)

# 将响应转换为字典
response_dict = r.json()  # 将调取内容转换为字典格式

# 处理结果
print(response_dict.keys())  # 打印字典所有键

Python从入门到实践project使⽤ API_处理CSV文件格式数据

获取仓库某个键的可获取信息

print(f"Total repositories: {response_dict['total_count']}")
# 打印total_count所对应的值,能够反应github上面有多少符合代码条件的仓库
print(f"Complete results: {not response_dict['incomplete_results']}")
# incomplete_results反映在有限时间内是否进行完整查询

# 探索有关仓库的信息
repo_dicts = response_dict['items']  # 查询仓库中某个列表
print(f"Repositories returned: {len(repo_dicts)}")  # 查询仓库中有多少个列表

# 研究第⼀个仓库
repo_dict = repo_dicts[0]   # 第一个键
print(f"\nKeys: {len(repo_dict)}")  # 第一个键数量
for key in sorted(repo_dict.keys()):
 print(key)                          # 第一个键盘可获取信息

Python从入门到实践project使⽤ API_github_02

打印具体键的具体信息

# 依次打印第一键相关信息
print("\nSelected information about first repository:")
print(f"Name: {repo_dict['name']}")
print(f"Owner: {repo_dict['owner']['login']}")
print(f"Stars: {repo_dict['stargazers_count']}")
print(f"Repository: {repo_dict['html_url']}")
print(f"Created: {repo_dict['created_at']}")
print(f"Updated: {repo_dict['updated_at']}")
print(f"Description: {repo_dict['description']}")

Python从入门到实践project使⽤ API_json_03

利用for循环依次打印需要获取的某个键的所有信息

print("\nSelected information about each repository:")
for repo_dict in repo_dicts:
 # 依次打印第一键相关信息
 print("\nSelected information about first repository:")
 print(f"Name: {repo_dict['name']}")
 print(f"Owner: {repo_dict['owner']['login']}")
 print(f"Stars: {repo_dict['stargazers_count']}")
 print(f"Repository: {repo_dict['html_url']}")
 print(f"Created: {repo_dict['created_at']}")
 print(f"Updated: {repo_dict['updated_at']}")
 print(f"Description: {repo_dict['description']}")

数据交互式可视化

import requests
import plotly.express as px


# 执⾏ API 调⽤并查看响应
url = "https://api.github.com/search/repositories"
url += "?q=language:python+sort:stars+stars:>10000"
headers = {"Accept": "application/vnd.github.v3+json"}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")

# 处理结果
response_dict = r.json()
print(f"Complete results: {not response_dict['incomplete_results']}")

# 处理有关仓库的信息
repo_dicts = response_dict['items']
repo_names, stars = [], []

for repo_dict in repo_dicts:
    repo_names.append(repo_dict['name'])  # 仓库名
    stars.append(repo_dict['stargazers_count'])  # 收藏数

# 可视化
fig = px.bar(x=repo_names, y=stars)  # 绘制条形图
fig.show()

Python从入门到实践project使⽤ API_API_04

设置标题及横纵坐标

title = "Most-Starred Python Projects on GitHub"
labels = {'x': 'Repository', 'y': 'Stars'}
fig = px.bar(x=repo_names, y=stars, title=title, labels=labels)  # bar绘制条形图
fig.update_layout(title_font_size=28, xaxis_title_font_size=20,yaxis_title_font_size=20)
title = "Most-Starred Python Projects on GitHub"
labels = {'x': 'Repository', 'y': 'Stars'}
fig = px.bar(x=repo_names, y=stars, title=title, labels=labels)  # bar绘制条形图
fig.update_layout(title_font_size=28, xaxis_title_font_size=20,yaxis_title_font_size=20)

Python从入门到实践project使⽤ API_处理GeoJSON 数据_05

创建悬停文字

repo_names, stars, hover_texts = [], [], []
# 创建悬停⽂本
owner = repo_dict['owner']['login']
description = repo_dict['description']
hover_text = f"{owner}<br />{description}"
hover_texts.append(hover_text)
fig = px.bar(x=repo_names, y=stars, title=title, labels=labels,hover_name=hover_texts)

Python从入门到实践project使⽤ API_API_06

根据仓库名字创建链接

repo_links, stars, hover_texts = [], [], []
# 将仓库名转换为链接
repo_name = repo_dict['name']
repo_url = repo_dict['html_url']
repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)

Python从入门到实践project使⽤ API_API_07

设置条形图颜色跟透明度

fig.update_traces(marker_color='SteelBlue', marker_opacity=0.6)

Python从入门到实践project使⽤ API_处理GeoJSON 数据_08

Hacker News API

创建代码

import requests
import json

# 执⾏ API 调⽤并存储响应
url = "https://hacker-news.firebaseio.com/v0/item/31353677.json"
r = requests.get(url)  # 获取数据
print(f"Status code: {r.status_code}")  # 打印状态码

# 探索数据的结构
response_dict = r.json()  # 数据转换为字典
response_string = json.dumps(response_dict, indent=4)  # 数据重新排版
print(response_string)  # 打印数据

Python从入门到实践project使⽤ API_json_09

获取每篇文章的相关信息

from operator import itemgetter
import requests

# 执⾏ API 调⽤并查看响应
url = "https://hacker-news.firebaseio.com/v0/topstories.json"
r = requests.get(url)
print(f"Status code: {r.status_code}")

# 处理有关每篇⽂章的信息
submission_ids = r.json()
submission_dicts = []  # 储存文章相关信息

for submission_id in submission_ids[:5]:    # 只循环列表前五个

    # 对于每篇⽂章,都执⾏⼀个 API 调⽤
    url = f"https://hacker-news.firebaseio.com/v0/item/{submission_id}.json"
    r = requests.get(url)
    print(f"id: {submission_id}\tstatus: {r.status_code}")  # 根据id获取信息
    response_dict = r.json()

    # 对于每篇⽂章,都创建⼀个字典
    submission_dict = {'title': response_dict['title'],
                       'hn_link': f"https://news.ycombinator.com/item?id={submission_id}",
                       'comments': response_dict['descendants'], }   # 包括标题链接字典

    submission_dicts.append(submission_dict)  # 字典添加到列表中

    submission_dicts = sorted(submission_dicts,key=itemgetter('comments'),reverse=True)
    # sorted对列表进行排序,key是根据评论数量排序,reverse倒叙排序

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']}")

Python从入门到实践project使⽤ API_github_10