和风查询天气

博客涉及Python程序与和风天气相关内容,但具体信息缺失。推测可能是用Python程序处理和风天气的数据或实现相关功能。
部署运行你感兴趣的模型镜像
import json, requests, sys
print('请输入您要查询的城市:')
loc=input()
#The key is used for my personal usage, please do not use it random, thx.
key1='38810bc1e40a401fb6ca6ff04a4f056b'
canshu={'location':loc,'key':key1}
#url=r'https://free-api.heweather.com/s6/weather/now?location=%s&key=38810bc1e40a401fb6ca6ff04a4f056b'%(location)
#response = requests.get(url)
response = requests.get(r'https://free-api.heweather.com/s6/weather/now',params=canshu)
response.raise_for_status()

#airquality=r'https://free-api.heweather.com/s6/air/now?location=%s&key=38810bc1e40a401fb6ca6ff04a4f056b'%(location)
#airqual=requests.get(airquality)
airqual=requests.get(r'https://free-api.heweather.com/s6/air/now',params=canshu)
# Load JSON data into a Python variable.
weatherData = json.loads(response.text)
print('天气数据:',weatherData)
airData=json.loads(airqual.text)
print('空气质量:',airData)
# Print weather descriptions.
w = weatherData['HeWeather6']
a = airData['HeWeather6']
#print('城市:%s' % (loc),'更新时间:'+w[0]['update']['loc'])
print('城市:'+w[0]['basic']['location'],'更新时间:'+w[0]['update']['loc'])
#print('城市:%s' % (location),'更新时间:'+w[0]['update']['loc'])
print('天气:'+w[0]['now']['cond_txt'],'  风向:'+w[0]['now']['wind_dir'])
print('体感温度:'+w[0]['now']['fl']+'℃','温度:'+w[0]['now']['tmp']+'℃')
print('空气质量:'+a[0]['air_now_city']['qlty'],'PM2.5:'+a[0]['air_now_city']['pm25'],'PM10:'+a[0]['air_now_city']['pm10'])

 

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

可以借助 Visual Studio 结合 `.NET` 的 `HttpClient` 类实现和风天气查询。以下为实现步骤和示例代码: ### 步骤 1. **创建项目**:打开 Visual Studio,创建一个新的 `.NET` 控制台应用程序项目。 2. **获取和风天气 API 密钥**:前往和风天气官网注册账号并获取 API 密钥。 3. **编写代码**:在项目的主文件(通常是 `Program.cs`)中编写代码,使用 `HttpClient` 类发送 HTTP 请求到和风天气 API 并处理响应。 ### 示例代码 ```csharp using System; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json.Linq; class Program { static async Task Main() { // 和风天气 API 的基本 URL string url = "https://api.heweather.net/v7/weather/now"; // 填写你在和风天气获取的 API 密钥 string apiKey = "your_api_key"; // 可替换为你想查询的城市 string location = "北京"; // 创建 HttpClient 实例 using (HttpClient client = new HttpClient()) { // 构建请求的 URL string requestUrl = $"{url}?key={apiKey}&location={location}"; try { // 发送 GET 请求 HttpResponseMessage response = await client.GetAsync(requestUrl); // 检查请求是否成功 if (response.IsSuccessStatusCode) { // 读取响应内容 string responseBody = await response.Content.ReadAsStringAsync(); // 解析 JSON 数据 JObject data = JObject.Parse(responseBody); // 提取关键天气信息 string obsTime = data["now"]["obsTime"].ToString().Split(' ')[0]; string temperature = data["now"]["temp"].ToString(); string weather = data["now"]["text"].ToString(); Console.WriteLine($"{obsTime} 当前温度:{temperature}°C,天气状况:{weather}"); } else { Console.WriteLine("获取天气数据失败,请检查网络或 API 密钥"); } } catch (Exception ex) { Console.WriteLine($"发生错误:{ex.Message}"); } } } } ``` ### 代码解释 - 引入必要的命名空间,包含网络请求和 JSON 处理相关的命名空间。 - 在 `Main` 方法中设置和风天气 API 的基本 URL、API 密钥和要查询的城市。 - 创建 `HttpClient` 实例,构建请求的 URL 并发送 GET 请求。 - 检查响应状态码,若请求成功则解析响应的 JSON 数据,提取关键天气信息并输出;若失败则输出错误信息。 ### 注意事项 - 请将 `your_api_key` 替换为你自己在和风天气官网获取的真实 API 密钥。 - 代码中使用了 `Newtonsoft.Json` 库来解析 JSON 数据,你可以通过 NuGet 包管理器安装该库。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值