用户:鸽子ECYRAZ查看:0 回复:0 评论:0 创建时间:2021-07-04T19:01:07
【作品展示】

【作品介绍】
网站名称:https://www.tianqiapi.com/api/?version=v6&appid=74169348&appsecret=ti3VzXtb&city=
【作品源代码】
#http协议:超文本传输协议:是一种勇于分布式、协作式和超媒体信息系统的应用层协议。万维网的数据通讯的基础
#requests:是python实现最简单易用的http库
#json:JavaScript Object Notation是一种轻量级数据交换格式。
#api:应用程序接口。实现提前定义好接口(函数、http)
#url
import requests
import json
#接收用户输入的城市
city_n=input("输入查询的城市")
#api接口拼接完整
url = 'https://www.tianqiapi.com/api/?version=v6&appid=74169348&appsecret=ti3VzXtb&city='+city_n
#访问天气接口获得完整的数据
resq=requests.get(url)
print(type(resq))
print(resq)
#获得返回的城市天气的json数据
json_data=resq.text
print(type(json_data))
print(json_data)
#将json数据转化为python字典
weather=json.loads(json_data)
#(1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
#(2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)
print("______________--")
print(weather)
print(type(weather))
#取出字典中的天气信息,展示(位置、今天日期、当天温度、当前天气、风速、风力、空气质量、建议)
print("当前位置:"+weather['city'])
print("今天日期:"+weather['date']+"当前温度:"+weather['tem']+"当前天气:"+weather['wea']+"风速:"+weather['win_meter']+"风力:"+weather['win_speed']+"空气质量:"+weather['air_level']+"建议:"+weather['air_tips'])
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn