Update README.md

main
hnu202501120112 2 months ago
parent 0947756a58
commit 111007c62d

@ -2,6 +2,8 @@
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)'
@ -14,52 +16,36 @@ def get_bs_object(url, headers):
bs = BeautifulSoup(response.text, 'html.parser')
return bs
except requests.exceptions.RequestException as e:
print(f"网络请求失败或URL无效{e}")
return None
def main():
while True:
timecity_input = input("请设置地点,格式为:省份+市+地区(用逗号分隔,如湖南,长沙,岳麓区)\n")
timecity = timecity_input.split('')
if len(timecity) < 2:
print("格式错误!请输入至少“省份,市”的格式,重新输入~")
continue
break
province = timecity[0].strip()
city = timecity[1].strip()
area = timecity[2].strip() if len(timecity) >= 3 else ""
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 not bs_province:
return
if isinstance(bs_province, str):
return bs_province
weathercity1 = bs_province.find_all('a', title=f"{province}天气")
if not weathercity1:
print(f"未找到{province}对应的天气链接,请检查省份名称是否正确!")
return
return f"未找到{province}对应的天气链接,请检查省份名称是否正确!"
city_href = weathercity1[0].get("href")
if not city_href:
print("未获取到城市对应的链接!")
return
return "未获取到城市对应的链接!"
city_url = f"{BASE_URL}/{city_href}"
bs_city = get_bs_object(city_url, COMMON_HEADER)
if not bs_city:
return
if isinstance(bs_city, str):
return bs_city
weathercity2 = bs_city.find_all('a', title=f"{city}天气")
if not weathercity2:
print(f"未找到{city}对应的天气链接,请检查城市名称是否正确!")
return
return f"未找到{city}对应的天气链接,请检查城市名称是否正确!"
city_path = weathercity2[0].get("href").split("/")
if len(city_path) < 3:
print("城市链接格式异常,无法继续!")
return
return "城市链接格式异常,无法继续!"
del (city_path[:2])
area_code = city_path[0] if city_path else ""
if not area_code:
print("未提取到地区编码,无法继续!")
return
return "未提取到地区编码,无法继续!"
target_url = f"{BASE_URL}/today-{area_code}"
bs_weather = get_bs_object(target_url, COMMON_HEADER)
if not bs_weather:
return
if isinstance(bs_weather, str):
return bs_weather
weekday_list = ["周一天气", "周二天气", "周三天气", "周四天气", "周五天气", "周六天气", "周日天气"]
today_index = int(datetime.datetime.now().weekday())
weekday_list[today_index] = "今天天气"
@ -76,22 +62,77 @@ def main():
f"/sixth-{area_code}",
f"/seventh-{area_code}"
]
print(f"\n可查询的日期{', '.join(weekday_list)}")
while True:
query_date = input("请输入要查询的天气日期(如:今天天气)\n").strip()
try:
query_index = weekday_list.index(query_date)
break
except ValueError:
print(f"输入无效!请从可查询日期中选择:{', '.join(weekday_list)},重新输入~")
continue
try:
query_index = weekday_list.index(query_date)
except ValueError:
return f"无效的查询日期!可查询日期:{', '.join(weekday_list)}"
today_index = weekday_list.index("今天天气")
relative_href = href_list[query_index - today_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:
print(f"未找到{query_date}对应的天气信息!")
return
return f"未找到{query_date}对应的天气信息!"
weather_info = ' '.join(weather_items[0].text.split())
print(f"\n{query_date} - {province}{city}{area}天气:{weather_info}")
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__":
main()
create_gui()
Loading…
Cancel
Save