大江网站建设,免费采集器 wordpress,网站开发需要掌握哪些知识,注册公司名字有没有重复在哪可以查Python 获取天气数据
检查天气似乎相当简单#xff1a;打开 Web 浏览器#xff0c;点击地址栏#xff0c; 输入天气网站的 URL(或搜索一个#xff0c;然后点击链接)#xff0c; 等待页面加载#xff0c;跳过所有的广告等。
其实#xff0c;如果有一个程序#xff0c;…Python 获取天气数据
检查天气似乎相当简单打开 Web 浏览器点击地址栏 输入天气网站的 URL(或搜索一个然后点击链接) 等待页面加载跳过所有的广告等。
其实如果有一个程序下载今后几天的天气预报并以 纯文本打印出来就可以跳过很多无聊的步骤。该程序利用 第11章介绍的 requests 模块从网站下载数据。 总的来说该程序将执行以下操作
从命令行读取请求的位置。
从OpenWeatherMap.org下载JSON天气数据。
将 JSON 数据字符串转换成 Python 的数据结构。
打印今天和未来两天的天气。
因此代码需要完成以下任务
连接 sys.argv 中的字符串得到位置。
调用 requests.get() 下载天气数据。
调用 json.loads()将JSON数据转换 为Python数据结构。
打印天气预报。
针对这个项目打开一个新的文件编辑器窗口 并保存为 quickWeather.py 。
第1步从命令行参数获取位置
该程序的输入来自命令行。让 quickWeather.py 看起来像这样
#! python3
quickWeather.py - Prints the weather for a location from the command line.
import json, requests, sys
Compute location from command line arguments.
if len(sys.argv) 2: print(‘Usage: quickWeather.py location’) sys.exit() location“长春” #T0D0: Download the JSON data from OpenWeatherMap.org’s API.
#T0D0: Load JSON data into a Python variable. 在 Python 中命令行参数存储在 sys.argv 列表里。 #! 行和 import 语句之后程序会检查是否有多个 命令行参数回想一下 sys.argv 中至少有一个元素 sys.argv[0] 它包含了Python 脚本的文件名。 如果该列表中只有一个元素那么用户没有在命 令行中提供位置程序向用户提供“Usage(用法)”信息 然后结束。
命令行参数以空格分隔。命令行参数San Francisco, CA将使 sys.argv中保存[‘quickWeather.py’,‘San’,‘Francisco’, ‘CA’]。 因此调用 join()方法将 sys.argv 中除第一个字符串以 外的字符串连接起来。将连接的字符串存储在变量location 中。
第2步下载 JSON 数据
OpenWeatherMap.org提供了 JSON 格式的实时天气信息。 你的程序只需要下载页面 http://api.openweathermap.org/data/2.5/forecast/daily?qcnt3 其中是想知道天气的城市。 将以下代码添加到 quickWeather.py 中。 #! python3 quickWeather.py - Prints the weather for a location from the command line. import json, requests, sys Compute location from command line arguments. if len(sys.argv) 2: print(‘Usage: quickWeather.py location’) sys.exit() location“长春” weatherJsonUrl “http://wthrcdn.etouch.cn/weather_mini?city%s” % (location) response requests.get(weatherJsonUrl) try: response.raise_for_status()except: print(网址请求出错)response requests.get(weatherJsonUrl) weatherData json.loads(response.text) w weatherData[‘data’] TODO: Load JSON data into a Python variable. 我们从命令行参数中得到了location 。为了生成要访问的网址 我们利用s占位符将 location 中保存的字符串插入 URL 字符串的那个位置。结果保存在 url 中并将 url 传入 requests.get() 。requests.get() 调用 返回一个Response 对象它可以通过调用 raise_for_status() 来检查错误。如果不发生异常 下载的文本将保存在 response.text 中。
第3步加载 JSON 数据并打印天气
response.text成员变量保存了一个JSON格式数据的 大字符串。要将它转换为Python值就调用json.loads() 函数。JSON数据会像这样
{‘city’:{‘coord’: {‘lat’: 37.7771, ‘Ion’: -122.42}, ‘country’: ’ United States of America’, ‘id’: ‘5391959’, ‘name’: ‘San Francisco’,‘population’: 0}, ‘cnt’:3, ‘cod’:200, ‘list’:[{‘clouds’: 0,‘deg’: 233,‘dt’: 1402344000,‘humidity’: 58,‘pressure’: 1012.23, speed1: 1.96, ‘temp’: {‘day’: 302.29, ‘eve’: 296.46, ‘max’: 302.29 ‘min’:289.77, ‘morn’: 294.59, ‘night’: 289.77}, ‘weather’: [{‘description’:‘sky is clear’, ‘icon’:‘01d’ –snip– 可以将weatherData传入pprint.pprint查看这个数据。 你可能要查找http://openweathermap.org/ 找到关于这些 字段含义的文档。例如在线文档会告诉你day’后面的 302.29是白天的开尔文温度而不是摄氏或华氏温度。
你想要的天气描述在’nain’和’description’之后。 为了整齐地打印出来在 quickWeather.py 中添加以下代码。 #! python3 quickWeather.py - Prints the weather for a location from the command line. import json, requests, sys Compute location from command line arguments. if len(sys.argv) 2: print(‘Usage: quickWeather.py location’) sys.exit() location“长春” weatherJsonUrl “http://wthrcdn.etouch.cn/weather_mini?city%s” % (location) response requests.get(weatherJsonUrl) try: response.raise_for_status()except: print(网址请求出错)response requests.get(weatherJsonUrl) weatherData json.loads(response.text) w weatherData[‘data’] print(“地点%s” % w[‘city’]) date_a [] highTemp [] lowTemp [] weather [] for i in range(len(w[‘forecast’])): date_a.append(w[‘forecast’][i][‘date’]) highTemp.append(w[forecast][i][high])lowTemp.append(w[forecast][i][low])weather.append(w[forecast][i][type])print(天气地点 %s: % (location))
print(日期 date_a[i])print(\t温度最 lowTemp[i] ℃~最 highTemp[i] ℃)print(\t天气 weather[i])print(\n今日着装 w[ganmao])print(当前温度 w[wendu] ℃)地点长春 天气地点 长春: 日期5日星期天 温度最低温 19℃℃~最高温 24℃℃ 天气中雨
今日着装感冒低发期天气舒适请注意多吃蔬菜水果多喝水哦。 当前温度19℃ 天气地点 长春: 日期6日星期一 温度最低温 18℃℃~最高温 26℃℃ 天气雷阵雨
今日着装感冒低发期天气舒适请注意多吃蔬菜水果多喝水哦。 当前温度19℃ 天气地点 长春: 日期7日星期二 温度最低温 17℃℃~最高温 27℃℃ 天气多云
今日着装感冒低发期天气舒适请注意多吃蔬菜水果多喝水哦。 当前温度19℃ 天气地点 长春: 日期8日星期三 温度最低温 18℃℃~最高温 29℃℃ 天气晴
今日着装感冒低发期天气舒适请注意多吃蔬菜水果多喝水哦。 当前温度19℃ 天气地点 长春: 日期9日星期四 温度最低温 20℃℃~最高温 31℃℃ 天气晴
今日着装感冒低发期天气舒适请注意多吃蔬菜水果多喝水哦。 当前温度19℃ 请注意代码将 weatherData[list’]保存在变量 w 中 这将节省一些打字时间。可以用 w[0] 、w[1]和 w[2] 来取得今天、明天和后天天气的字典。这些字典都有 ‘weather’ 键其中包含一个列表值。你感兴趣的是 第一个列表项一个嵌套的字典包含几个键下标是0。 这里我们打印出保存在’main’和’description’键 中的值用连字符隔开。
如果用命令行参数quickWeather.py San Francisco,CA 运行这个程序输出看起来是这样的
Current weather in San Francisco, CA: Clear - sky is clear
Tomorrow: Clouds - few clouds
Day after tomorrow: Clear - sky is clear