【米尔王牌产品MYD-Y6ULX-V2开发板】天气预报——基于python
发布日期:2023.1.14 浏览次数:1985 次 |
由评测者“华仔stm32”提供。
利用网络获取天气预报,先要找到api,我这里在网上找到了一个链接。
新建weather.py文件,内容如下:
# 导入json、requests包 import json import requests # 请求天气信息 weather_url = "http://www.tianqiapi.com/api?version=v6&appid=23035354&appsecret=8YvlPNrz" response = requests.get(weather_url) try: response.raise_for_status() except response.HTTPError: print("请求信息出错") # 将json文件格式导入成python的格式 response.encoding = response.apparent_encoding weather_data = json.loads(response.text) print("地点:%s" % weather_data["city"]) print("日期:" + weather_data["date"]) print("\t温度:" + weather_data["tem"]) print("\t天气:" + weather_data["wea"] + "\n")
这里需要安装requests模块,pip3 install 是安装不了的,需要这样安装:
pip3 install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com requests
安装好模块后,执行python3 weather.py。效果如下:
root@myd-y6ull14x14:~# python3 weather.py 地点:桂林 日期:2022-12-04 温度:7 天气:多云