2022
02-10
02-10
Python脚本制作天气查询实例代码
获取天气的主要代码如下:#cityCode替换为具体某一个城市的对应编号#1、发送请求,获取数据url=f'http://wthrcdn.etouch.cn/weather_mini?citykey={cityCode}'res=requests.get(url)res.encoding='utf-8'res_json=res.json()#2、数据格式化data=res_json['data']city=f"城市:{data['city']}\n"#字符串格式化的一种方式f"{}"通过字典传递值today=data['forecast'][0]date=f"日期:{today['date']}\...
继续阅读 >
一、实现效果1.python代码importrequestsfromlxmlimportetreeimportreimporttkinterastkfromPILimportImage,ImageTkfromxpinyinimportPinyindefget_image(file_nam,width,height):im=Image.open(file_nam).resize((width,height))returnImageTk.PhotoImage(im)defspider():headers={'user-agent':'Mozilla/5.0(WindowsNT6.2;WOW64)AppleWebKit/535.24(KHTML,likeGecko)Chrome/19.0....
天气查询python小程序第0步:导入工具库第一步:生成查询天气的url链接第二步:访问url链接,解析服务器返回的json数据,变成python的字典数据第三步:对字典进行索引,获取气温、风速、风向等天气信息第四步:遍历forecast列表中的五个元素,打印天气信息完整Python代码本案例是一个非常有趣的python小程序,调用网络API查询指定城市的天气,并打印输出天气信息。你将学到以下技能:向网络API发起请求,解析和处理服务器返回的jso...