获取当年所有天气有雨的日期和天气

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)
 

#基本逻辑

循环发送网络请求并带回分析假如文字中带有雨,输出

剩下的自己去玩吧

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值