获取天气信息的程序Python
时间: 2024-03-30 17:26:38 浏览: 23
以下是一个获取天气信息的Python程序,使用了第三方库requests和json:
```python
import requests
import json
API_KEY = "YOUR_API_KEY" # 替换为你的API KEY
CITY_NAME = "Beijing" # 替换为你要查询的城市名称
# 发送请求并获取响应
response = requests.get(f"http://api.openweathermap.org/data/2.5/weather?q={CITY_NAME}&appid={API_KEY}")
# 解析JSON格式的响应数据
weather_data = json.loads(response.text)
# 获取温度和天气情况
temperature = round(weather_data["main"]["temp"] - 273.15, 1) # 转换为摄氏度并保留一位小数
weather = weather_data["weather"][0]["description"]
# 输出结果
print(f"{CITY_NAME}天气:{weather},温度:{temperature}℃")
```
需要注意的是,这里使用的是OpenWeatherMap提供的API,需要先注册并获取自己的API KEY。另外,查询的城市名称需要根据OpenWeatherMap的命名规范来写。
相关问题
编写爬取天气的python程序
以下是一个简单的Python程序,可以从网站上爬取天气信息:
```python
import requests
from bs4 import BeautifulSoup
# 设置要爬取的城市和日期
city = 'beijing'
date = '2019-01-01'
# 构造URL
url = f'http://www.tianqihoubao.com/lishi/{city}/month/{date}.html'
# 发送请求,获取页面内容
res = requests.get(url)
res.encoding = 'gbk' # 设置编码为gbk,因为该网站使用gbk编码
html = res.text
# 使用BeautifulSoup解析页面内容
soup = BeautifulSoup(html, 'html.parser')
# 获取表格元素
table = soup.find('table', class_='b')
# 解析表格数据,获取每天的天气信息
for tr in table.find_all('tr')[1:]:
tds = tr.find_all('td')
day = tds[0].text.strip()
desc = tds[1].text.strip()
temp = tds[2].text.strip()
wind = tds[3].text.strip()
print(f'{day} {desc} {temp} {wind}')
```
该程序使用requests库发送请求,然后使用BeautifulSoup解析页面内容,最后解析表格数据获取每天的天气信息,并打印出来。请注意,该程序只是一个示例,实际应用中需要根据具体情况进行修改。
写一段获取天气Python代码
以下是一个获取天气的Python代码示例,需要使用第三方库requests和json:
```python
import requests
import json
# 输入城市名
city = input("请输入城市名:")
# API地址
url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}"
# 发送请求并获取响应
response = requests.get(url)
# 将响应内容转换为JSON格式
data = json.loads(response.text)
# 提取天气信息
if data.get("desc") == "OK":
forecast = data.get("data").get("forecast")[0]
date = forecast.get("date")
high = forecast.get("high")
low = forecast.get("low")
type = forecast.get("type")
print(f"{city} {date} 天气:{type},最高温度:{high},最低温度:{low}")
else:
print(data.get("desc"))
```
运行代码后,程序会要求输入城市名,然后会返回该城市当天的天气信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)