import requests
from bs4 import BeautifulSoup
# from lxml import etree
inyear = input("请输入年:")
# 开始
xiayuriqi=[99]
xiayutianqi=[]
#202201
yeartou=int(inyear+'01')
# print(yeartou)
shu=yeartou
tianqi='雨'
#要判断的天气
for year_month in range(1,13):
# print("当前年月%s" % shu)
url = 'https://www.tianqi24.com/changsha/history%s.html'%shu
# print(url)
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
element = soup.select_one('#main > section > article:nth-child(2) > section > ul')
# print(element)
# 如果找到了对应的元素,就遍历其子元素 li,并输出其文本内容
if element:
for li in element.find_all('li'):
divs =li.find_all('div')
first_div_text = divs[0].text.strip()
third_div_text = divs[1].text.strip()
# print(f"日期为 {first_div_text}")
# print(f"当天天气为 {third_div_text}")
dangqianzhongyao=str(first_div_text+''+third_div_text)
newdangqianzhongyao=(dangqianzhongyao.replace("\n","")).rstrip()+"\n"
# print(newdangqianzhongyao)
if tianqi in newdangqianzhongyao:
print(newdangqianzhongyao)
shu+=1
# print("当前年月%s"%shu)
#基本逻辑
循环发送网络请求并带回分析假如文字中带有雨,输出
剩下的自己去玩吧