大家好,我是python222_小锋老师,分享一套优质的基于Python的天气预报数据可视化分析系统(Flask+echarts+爬虫) 。
项目简介
随着气候变化的加剧,准确和时效的气象数据成为了日常出行的关键信息。本论文介绍了基于大数据技术的天气数据分析系统的设计与实现。该系统获取和风天气网获取实时天气数据,并经过清洗后存储在MySQL数据库中。利用ECharts技术实现数据可视化,展示了基本的天气信息和综合全国的天气数据。此外,系统。另外,系统具备用户登录、注册以及数据管理功能,用于管理和修改用户数据。总体而言,本系统实现了天气数据的自动获取、处理和可视化分析,同时提供了用户管理和数据管理功能。该系统不仅具有实用价值,也为未来气象数据研究提供了有价值的数据来源。
源码下载
链接: pan.baidu.com/s/1eL4elN6r…
提取码: 1234
相关截图
核心代码
import pymysql
import requests
import json
# 数据库连接配置
db_config = {
"host": "localhost",
"user": "root",
"password": "123456",
"database": "tianqi",
}
# 读取中国城市编码数据
def read_china_city_codes():
with open("china.json", "r", encoding="utf-8") as file:
data = json.load(file)
return data
# 爬取当前天气数据
def crawl_current_weather(city_id):
base_url = "https://devapi.qweather.com/v7/weather/now"
key = "05751994771f46a0bd1aee45a552de14"
full_url = f"{base_url}?location={city_id}&key={key}"
print(full_url)
try:
response = requests.get(full_url)
data = response.json()
if data["code"] == "200":
return data["now"]
else:
print(f"Error: {data['code']}, {data['message']}")
return None
except Exception as e:
print(f"Error: {e}")
return None
# 将当前天气数据保存到数据库
def save_current_weather_to_database(city_name, current_weather_data):
try:
connection = pymysql.connect(**db_config)
cursor = connection.cursor()
sql = '''
INSERT INTO weatherdata (
城市, 时间, 温度, 体感温度, 天气情况,
风力等级, 湿度, 能见度
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
'''
values = (
city_name,
current_weather_data["obsTime"],
current_weather_data["temp"],
current_weather_data["feelsLike"],
current_weather_data["text"],
current_weather_data["windScale"],
current_weather_data["humidity"],
current_weather_data["vis"],
)
cursor.execute(sql, values)
connection.commit()
print(f"当前天气数据保存成功: {city_name}")
except pymysql.Error as err:
print(f"数据库错误: {err}")
finally:
if connection:
cursor.close()
connection.close()
if __name__ == "__main__":
# 读取中国城市编码数据
china_city_codes = read_china_city_codes()
if china_city_codes:
for city_name, city_id in china_city_codes.items():
# 获取当前天气信息
current_weather_data = crawl_current_weather(city_id)
if current_weather_data:
# 将当前天气数据保存到数据库
save_current_weather_to_database(city_name, current_weather_data)