diff --git a/README.md b/README.md index 6376748..fb80e72 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,97 @@ # python - +import requests +from bs4 import BeautifulSoup +import datetime +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: + 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 "" + province_url = f"{BASE_URL}/china.htm" + bs_province = get_bs_object(province_url, COMMON_HEADER) + if not bs_province: + return + weathercity1 = bs_province.find_all('a', title=f"{province}天气") + if not weathercity1: + print(f"未找到{province}对应的天气链接,请检查省份名称是否正确!") + return + city_href = weathercity1[0].get("href") + if not city_href: + print("未获取到城市对应的链接!") + return + city_url = f"{BASE_URL}/{city_href}" + bs_city = get_bs_object(city_url, COMMON_HEADER) + if not bs_city: + return + weathercity2 = bs_city.find_all('a', title=f"{city}天气") + if not weathercity2: + print(f"未找到{city}对应的天气链接,请检查城市名称是否正确!") + return + city_path = weathercity2[0].get("href").split("/") + if len(city_path) < 3: + print("城市链接格式异常,无法继续!") + return + del (city_path[:2]) + area_code = city_path[0] if city_path else "" + if not area_code: + print("未提取到地区编码,无法继续!") + return + target_url = f"{BASE_URL}/today-{area_code}" + bs_weather = get_bs_object(target_url, COMMON_HEADER) + if not bs_weather: + return + 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}" + ] + 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 + today_index = weekday_list.index("今天天气") + relative_href = href_list[query_index - today_index] + weather_items = bs_weather.find_all('a', href=relative_href, title=query_date) + if not weather_items: + print(f"未找到{query_date}对应的天气信息!") + return + weather_info = ' '.join(weather_items[0].text.split()) + print(f"\n{query_date} - {province}{city}{area}天气:{weather_info}") +if __name__ == "__main__": + main()