You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.3 KiB
84 lines
2.3 KiB
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 = "3815d44304a2468f817ea3b41882f20a"
|
|
|
|
full_url = f"{base_url}?location={city_id}&key={key}"
|
|
|
|
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)
|