asp.net 调用python python调用网站api

您所在的位置:网站首页 如何安装pygal asp.net 调用python python调用网站api

asp.net 调用python python调用网站api

2023-07-11 06:36| 来源: 网络整理| 查看: 265

python编程从入门到实践-使用API1. 使用WEB API1.1 使用API调用请求数据1.2. 安装 resuests1.3.处理API响应1.4.处理响应字典1.5. 概述最受欢迎的仓库1.6.监视API的速率限制2. 使用Pygal可视化仓库2.1 添加自定义工具提示2.2 在图标中添加可单机的链接3 总结3.1 API 调用:3.2 可视化

1. 使用WEB API1.1 使用API调用请求数据

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7TvWGgC8-1609325517381)(F4BACDF3A24F44B0B39009BEF86A44A8)]

1.2. 安装 resuests1.3.处理API响应

python_repos.py

''' --coding utf-8-- created on Dec 28,2020 python crash course ''' 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) #需要判断当前网页对于我们的请求是否有响应 ,比如200代表服务器正常响应, 404代表页面未找到 #讲API响应存储在一个变量中 #API返回JSON格式,使用方法json() 将这些信息转换为Python字典 response_dict=r.json() #处理结果 print(response_dict.keys())

结果: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S9tO3GR0-1609325517383)(A1BCB196AC7D41229ABE9456137691C7)]

1.4.处理响应字典

将API调用返回的信息存储到字典中后,就可以处理这个字典中的数据了。

pyton_repos.py

# 探索有关仓库的信息 repo_dicts=response_dict['items'] #items 是一个列表,其中包含很多字典,每个字典都包含一个python 仓库的信息 print(repo_dicts) print("Repositorise returned:",len(repo_dicts)) #研究第一个仓库 repo_dict=repo_dicts[0] print("\nkeys:",len(repo_dict)) #打印字典中包含的键,查看包含哪些信息 for key in sorted(repo_dict.keys()): print(key)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MaGCrzrU-1609325517385)(9410591525644853BC35CEDC6F8E3523)]

1.5. 概述最受欢迎的仓库

编写一个循环,打印API调用返回的每个仓库的特定信息,以便可视化

for repo_dict in repo_dicts: print('\nName:',repo_dict['name']) print('Stars:',repo_dict['stargazers_count'])1.6.监视API的速率限制

大多数API存在速率限制,即在特定的时间内可执行的请求数存在限制 了解githubde 限制:在浏览器中输入

https://api.github.com/rate_limit

{ “resources”: “core”:{“limit”:60,“remaining”:59,“reset”:1609239743,“used”:1},“graphql”:{“limit”:0,“remaining”:0,“reset”:1609239773,“used”:0},“integration_manifest”:{“limit”:5000,“remaining”:5000,“reset”:1609239773,“used”:0}, “search”: {“limit”:10,“remaining”:10,“reset”:1609236233,“used”:0}}, “rate”: {“limit”:60,“remaining”:59,“reset”:1609239743,“used”:1}}

主要关心的是搜索的速率限制,即 search 中的参数;用完配额后,将收到一条简单的响应,由此可知已达到API极限,到达极限后,必须等待配额重置

2. 使用Pygal可视化仓库

呈现Github上Python项目的受欢迎程度 创建一个交互式的条形图,条形图的高度表示项目获得了多少颗星,单机条形将进入Github上的主页。

pygal:python 数据可视化工具 特点:pygal比较小众,「注于SVG图」,「擅长交互」,可弥补matplotlib和seborn这方面的缺陷问题:用matplotlib是不是也一样import requests import pygal from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS #执行API调用并存储响应 url="https://api.github.com/search/repositories?q=language:python&sort=stars" r=requests.get(url) print("status code:",r.status_code) #需要判断当前网页对于我们的请求是否有响应 ,比如200代表服务器正常响应, 404代表页面未找到 #讲API响应存储在一个变量中 #API返回JSON格式,使用方法json() 将这些信息转换为Python字典 response_dict=r.json() print("total repositories:",response_dict['total_count']) # 探索有关仓库的信息 repo_dicts=response_dict['items'] names,stars=[],[] for repo_dict in repo_dicts: names.append(repo_dict['name']) stars.append(repo_dict['stargazers_count']) #可视化 mystyle=LS('#333366',base_style=LCS) chart=pygal.Bar(style=mystyle,x_label_rotation=45,show_legend=False) #show_legend=False:隐藏图例 chart.title="Most-starred Pyhton Projects on Github" chart.x_labels=names chart.add('',stars) chart.render_to_file('python_repos.svg')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1j4nLEQo-1609325517388)(4519B765ACA84FDEA13A972A179B67B6)]

2.1 添加自定义工具提示

在Pygal中,将鼠标指向条形将显示它表示的信息,这通常称为工具提示。向chart.add()传递一个字典列表,而不是值列表

names,plot_dicts=[],[] for repo_dict in repo_dicts: names.append(repo_dict['name']) #检查description 是否为空,若为空则填充 description = repo_dict['description'] if not description: description='No description proviede' plot_dict={'value':repo_dict['stargazers_count'], 'label':str(repo_dict['description']), } plot_dicts.append(plot_dict) chart.add('',plot_dicts) chart.render_to_file('python_repose1.svg')2.2 在图标中添加可单机的链接names,plot_dicts=[],[] for repo_dict in repo_dicts: names.append(repo_dict[‘name’]) #检查description 是否为空,若为空则填充 description = repo_dict[‘description’] if not description: description=‘No description proviede’plot_dict={'value':repo_dict['stargazers_count'], 'label':str(repo_dict['description']), 'xlink':repo_dict['html_url'] } plot_dicts.append(plot_dict)

asp.net 调用python python调用网站api_API

3 总结3.1 API 调用:url=‘ ’ r=requests.get(url) print("status code:",r.status_code) #需要判断当前网页对于我们的请求是否有响应 ,比如200代表服务器正常响应, 404代表页面未找到 #讲API响应存储在一个变量中 #API返回JSON格式,使用方法json() 将这些信息转换为Python字典 response_dict=r.json()3.2 可视化

pygal:python 数据可视化工具 特点:pygal比较小众,「注于SVG图」,「擅长交互」,可弥补matplotlib和seborn这方面的缺陷



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3