Delete 'SpriderWindow.py'

main
pi6wzpflv 6 months ago
parent 0a6eb7ffaf
commit 6dba599308

@ -1,105 +0,0 @@
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()
Loading…
Cancel
Save