import requests
from bs4 import BeautifulSoup
import openpyxl
import re
#查找日期正则表达式
find_date = re.compile(r'
months = ["%d%02d" % (year, month + 1) for month in range(12)]
todo_urls = [f"http://lishi.tianqi.com/{cityname}/{month}.html" for month in months]
res = requests.get(url, headers=headers)
res = res.content.decode("utf-8")
soup = BeautifulSoup(res, "html.parser")
data = soup.find_all('ul',class_="lishitable_content clearfix")
dates = re.findall(find_date,data)
dtemp = re.findall(find_dtemp,data)
htemp = re.findall(find_htemp,data)
tianqi = re.findall(find_tianqi,data)
fengli = re.findall(find_fengli,data)
datalist.append([dates[i],dtemp[i],htemp[i],tianqi[i],fengli[i]])
def saveData(datalist,cityname,year):
wb.save("%d年%s历史天气.xlsx"%(year,cityname))
print("-------历史天气查询系统-------")
cityname = input("请输入城市名称(小写全拼):")
saveData(result,cityname,year)