parent
7fef0a5f61
commit
e277e58ce1
@ -0,0 +1,106 @@
|
||||
import os
|
||||
import tkinter as tk
|
||||
from tkinter import simpledialog
|
||||
from ttkbootstrap import Style
|
||||
import requests
|
||||
from lxml import etree
|
||||
import csv
|
||||
|
||||
|
||||
# 保持原有get_data函数不变
|
||||
def get_data(city, year_month):
|
||||
url = f"https://lishi.tianqi.com/{city}/{year_month}.html"
|
||||
weather_info = []
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
|
||||
}
|
||||
try:
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
html = etree.HTML(response.text)
|
||||
thrui_list = html.xpath("//ul[@class='thrui']/li")
|
||||
for li in thrui_list:
|
||||
day_weather_info = {}
|
||||
day_weather_info['day_time'] = li.xpath("./div[1]/text()")[0].split(" ")[0]
|
||||
high = li.xpath("./div[2]/text()")[0].split(" ")[0]
|
||||
day_weather_info['high'] = high[:high.find('C')]
|
||||
low = li.xpath("./div[3]/text()")[0].split(" ")[0]
|
||||
day_weather_info['low'] = low[:low.find('C')]
|
||||
day_weather_info['weather'] = li.xpath("./div[4]/text()")[0]
|
||||
day_weather_info['wind'] = li.xpath("./div[5]/text()")[0]
|
||||
weather_info.append(day_weather_info)
|
||||
except requests.RequestException as e:
|
||||
print(f"请求错误:{e}")
|
||||
return weather_info
|
||||
|
||||
|
||||
# 添加一个简单的城市中文到英文映射表
|
||||
city_to_english = {
|
||||
"长沙": "changsha",
|
||||
"株洲": "zhuzhou",
|
||||
"湘潭": "xiangtan",
|
||||
"衡阳": "hengyang",
|
||||
"邵阳": "shaoyang",
|
||||
"岳阳": "yueyang",
|
||||
"常德": "changde",
|
||||
"张家界": "zhangjiajie",
|
||||
"益阳": "yiyang",
|
||||
"郴州": "chenzhou",
|
||||
"永州": "yongzhou",
|
||||
"怀化": "huaihua",
|
||||
"娄底": "loudi",
|
||||
|
||||
# 添加更多城市...
|
||||
}
|
||||
|
||||
|
||||
def on_submit(city_entry):
|
||||
city = city_entry.get()
|
||||
if city in city_to_english:
|
||||
city = city_to_english[city]
|
||||
else:
|
||||
result_label.config(text=f"未找到城市 {city} 对应的英文映射,请输入正确的城市名")
|
||||
return
|
||||
|
||||
weathers = []
|
||||
|
||||
for month in range(1, 13):
|
||||
weather_time = "2023" + ('0' + str(month) if month < 10 else str(month))
|
||||
weather = get_data(city, weather_time)
|
||||
weathers.extend(weather)
|
||||
|
||||
with open(f'{city}天气.csv', 'w', newline='') as csvfile:
|
||||
writer = csv.writer(csvfile)
|
||||
writer.writerow(["日期", "最高气温", "最低气温", "天气", "风向/风速"])
|
||||
for day_weather in weathers:
|
||||
writer.writerow(list(day_weather.values()))
|
||||
result_label.config(text=f"{city}的天气数据已成功保存至'{city}天气.csv'")
|
||||
|
||||
|
||||
def main():
|
||||
global result_label
|
||||
|
||||
root = tk.Tk()
|
||||
|
||||
# # 置顶栏
|
||||
# music_path = os.path.join(os.path.abspath("__try__"), "安河桥.mp3")
|
||||
# play_music(music_path)
|
||||
|
||||
root.title("天气数据爬取工具")
|
||||
root.geometry("600x400+100+100")
|
||||
tk.Label(root, text="请输入城市名:", font=("Arial", 14, "bold"), pady=10).pack()
|
||||
city_entry = tk.Entry(root, font=("Arial", 14, "bold"), relief="flat")
|
||||
city_entry.pack()
|
||||
style = Style()
|
||||
style = Style(theme='yeti')
|
||||
submit_button = tk.Button(root, text="提交", width=8, command=lambda: on_submit(city_entry))
|
||||
submit_button.pack()
|
||||
|
||||
result_label = tk.Label(root, text="")
|
||||
result_label.pack()
|
||||
|
||||
root.mainloop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,35 @@
|
||||
# -*- coding: gbk -*-
|
||||
from typing import List
|
||||
|
||||
import mysql
|
||||
import mysql.connector
|
||||
import csv
|
||||
|
||||
db_config = {
|
||||
'host': 'localhost',
|
||||
'user': 'root',
|
||||
'password': '21412030115',
|
||||
'database': '长沙天气',
|
||||
}
|
||||
|
||||
cnx = mysql.connector.connect(**db_config)
|
||||
cursor = cnx.cursor()
|
||||
|
||||
# 读取CSV
|
||||
with open('changsha天气.csv', mode='r', encoding='gbk') as file:
|
||||
reader = csv.reader(file)
|
||||
headers = next(reader, None)
|
||||
if headers is not None: # 过表头
|
||||
for row in reader:
|
||||
sql = "INSERT IGNORE INTO 长沙天气(date, high_temperature, low_temperature, weather,fengsu) VALUES (%s, %s, %s, %s,%s)"
|
||||
cursor.execute(sql, (row[0], row[1], row[2], row[3], row[4])) #设csv与数据库对应
|
||||
|
||||
try:
|
||||
|
||||
cnx.commit()
|
||||
print("数据导入成功!")
|
||||
except mysql.connector.Error as err:
|
||||
print(f"数据导入失败: {err}")
|
||||
finally:
|
||||
cursor.close()
|
||||
cnx.close()
|
@ -0,0 +1,45 @@
|
||||
import mysql.connector
|
||||
import csv
|
||||
|
||||
# MySQL数据库配置
|
||||
db_config = {
|
||||
'host': 'localhost',
|
||||
'user': 'root',
|
||||
'password': '21412030115',
|
||||
'database': '长沙天气'
|
||||
}
|
||||
|
||||
# 连接到MySQL数据库
|
||||
cnx = mysql.connector.connect(**db_config)
|
||||
cursor = cnx.cursor()
|
||||
|
||||
# 创建表(如果尚未创建)
|
||||
# cursor.execute("""
|
||||
# CREATE TABLE IF NOT EXISTS weather_data (
|
||||
# id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
# date DATE,
|
||||
# high_temperature FLOAT,
|
||||
# low_temperature FLOAT,
|
||||
# weather VARCHAR(255),
|
||||
# wind_direction VARCHAR(255)
|
||||
# )
|
||||
# """)
|
||||
|
||||
# CSV文件路径
|
||||
csv_file_path = 'changsha天气.csv'
|
||||
|
||||
# 读取CSV文件并插入数据
|
||||
with open(csv_file_path, mode='r', encoding='gbk') as csvfile:
|
||||
csv_reader = csv.reader(csvfile)
|
||||
next(csv_reader) # 跳过表头
|
||||
for row in csv_reader:
|
||||
sql = "INSERT INTO weather_data (date, high_temperature, low_temperature, weather, wind_direction) VALUES (%s, %s, %s, %s, %s)"
|
||||
cursor.execute(sql, row)
|
||||
|
||||
# 提交事务
|
||||
cnx.commit()
|
||||
print("数据导入成功!")
|
||||
|
||||
# 关闭连接
|
||||
cursor.close()
|
||||
cnx.close()
|
After Width: | Height: | Size: 202 KiB |
After Width: | Height: | Size: 217 KiB |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue