ADD file via upload

main
hnu202501120112 2 months ago
parent 99ee5ab8ea
commit e2b9725621

@ -0,0 +1,137 @@
import requests
from bs4 import BeautifulSoup
import datetime
import tkinter as tk
from tkinter import messagebox, ttk
BASE_URL = "https://tianqi.2345.com"
COMMON_HEADER = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8)'
}
def get_bs_object(url, headers):
try:
response = requests.get(url=url, headers=headers, timeout=10)
response.raise_for_status()
response.encoding = "utf-8"
bs = BeautifulSoup(response.text, 'html.parser')
return bs
except requests.exceptions.RequestException as e:
return f"网络请求失败或URL无效{e}"
def query_weather(province, city, area, query_date):
province_url = f"{BASE_URL}/china.htm"
bs_province = get_bs_object(province_url, COMMON_HEADER)
if isinstance(bs_province, str):
return bs_province
weathercity1 = bs_province.find_all('a', title=f"{province}天气")
if not weathercity1:
return f"未找到{province}对应的天气链接,请检查省份名称是否正确!"
city_href = weathercity1[0].get("href")
if not city_href:
return "未获取到城市对应的链接!"
city_url = f"{BASE_URL}/{city_href}"
bs_city = get_bs_object(city_url, COMMON_HEADER)
if isinstance(bs_city, str):
return bs_city
weathercity2 = bs_city.find_all('a', title=f"{city}天气")
if not weathercity2:
return f"未找到{city}对应的天气链接,请检查城市名称是否正确!"
city_path = weathercity2[0].get("href").split("/")
if len(city_path) < 3:
return "城市链接格式异常,无法继续!"
del (city_path[:2])
area_code = city_path[0] if city_path else ""
if not area_code:
return "未提取到地区编码,无法继续!"
target_url = f"{BASE_URL}/today-{area_code}"
bs_weather = get_bs_object(target_url, COMMON_HEADER)
if isinstance(bs_weather, str):
return bs_weather
weekday_list = ["周一天气", "周二天气", "周三天气", "周四天气", "周五天气", "周六天气", "周日天气"]
today_index = int(datetime.datetime.now().weekday())
weekday_list[today_index] = "今天天气"
try:
weekday_list[today_index + 1] = "明天天气"
except IndexError:
weekday_list[0] = "明天天气"
href_list = [
f"/today-{area_code}",
f"/tomorrow-{area_code}",
f"/third-{area_code}",
f"/fourth-{area_code}",
f"/fifth-{area_code}",
f"/sixth-{area_code}",
f"/seventh-{area_code}"
]
try:
query_index = weekday_list.index(query_date)
except ValueError:
return f"无效的查询日期!可查询日期:{', '.join(weekday_list)}"
today_index = weekday_list.index("今天天气")
try:
relative_href = href_list[query_index - today_index]
except IndexError:
return "查询日期超出范围!"
weather_items = bs_weather.find_all('a', href=relative_href, title=query_date)
if not weather_items:
return f"未找到{query_date}对应的天气信息!"
weather_info = ' '.join(weather_items[0].text.split())
return f"{query_date} - {province}{city}{area}天气:{weather_info}"
def create_gui():
# 主窗口配置
root = tk.Tk()
root.title("简易天气查询工具")
root.geometry("600x400")
root.resizable(False, False)
frame1 = ttk.Frame(root, padding=10)
frame1.pack(fill="x")
ttk.Label(frame1, text="地区输入:").grid(row=0, column=0, padx=5, pady=5)
province_entry = ttk.Entry(frame1, width=10)
province_entry.grid(row=0, column=1, padx=5, pady=5)
province_entry.insert(0, "湖南")
ttk.Label(frame1, text="").grid(row=0, column=2, padx=2, pady=5)
city_entry = ttk.Entry(frame1, width=10)
city_entry.grid(row=0, column=3, padx=5, pady=5)
city_entry.insert(0, "长沙")
ttk.Label(frame1, text="").grid(row=0, column=4, padx=2, pady=5)
area_entry = ttk.Entry(frame1, width=10)
area_entry.grid(row=0, column=5, padx=5, pady=5)
area_entry.insert(0, "岳麓区")
ttk.Label(frame1, text="地区(可选)").grid(row=0, column=6, padx=2, pady=5)
frame2 = ttk.Frame(root, padding=10)
frame2.pack(fill="x")
ttk.Label(frame2, text="日期选择:").grid(row=0, column=0, padx=5, pady=5)
weekday_list = ["周一天气", "周二天气", "周三天气", "周四天气", "周五天气", "周六天气", "周日天气"]
today_index = int(datetime.datetime.now().weekday())
weekday_list[today_index] = "今天天气"
try:
weekday_list[today_index + 1] = "明天天气"
except IndexError:
weekday_list[0] = "明天天气"
date_var = tk.StringVar(value=weekday_list[today_index])
date_combo = ttk.Combobox(frame2, textvariable=date_var, values=weekday_list, width=20, state="readonly")
date_combo.grid(row=0, column=1, padx=5, pady=5)
frame3 = ttk.Frame(root, padding=10)
frame3.pack(fill="x")
def on_query_click():
province = province_entry.get().strip()
city = city_entry.get().strip()
area = area_entry.get().strip()
query_date = date_var.get().strip()
if not province or not city:
messagebox.showwarning("输入警告", "省份和城市不能为空!")
return
result = query_weather(province, city, area, query_date)
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, result)
query_btn = ttk.Button(frame3, text="查询天气", command=on_query_click)
query_btn.pack()
frame4 = ttk.Frame(root, padding=10)
frame4.pack(fill="both", expand=True)
ttk.Label(frame4, text="查询结果:").pack(anchor="w")
result_text = tk.Text(frame4, width=70, height=10, font=("宋体", 12))
scrollbar = ttk.Scrollbar(frame4, orient="vertical", command=result_text.yview)
result_text.configure(yscrollcommand=scrollbar.set)
result_text.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
root.mainloop()
if __name__ == "__main__":
create_gui()
Loading…
Cancel
Save