用PYTHON获取7天最高温度

import requests
from bs4 import BeautifulSoup
import re

url ='http://www.weather.com.cn/weather/101190101.shtml'
r = requests.get(url)
r.encoding ='utf8'
soup =BeautifulSoup(r.text,'html.parser')
body = soup.body
list_temp = body.find('ul', {'class':'t clearfix'})
li = list_temp.find_all('li')
final = []
#print(li)
for day in li: # 对每个li标签中的内容进行遍历
        temp = []
        date = day.find('h1').string # 找到日期
        temp.append(date)
        inf = day.find_all('p') # 找到li标签中所有的p标签
        temp.append(inf[0].string) # 将第一个p标签中的内容(天气状况)加入到temp中

        if inf[1].find('span') is None:
            temperature_higgest = None # 天气预报可能没有当天的最高气温(到了傍晚,就是这样),需要加一个判断,来输出最低气温
        else:
            temperature_higgest = inf[1].find('span').string # 找到最高气温
        temp.append(temperature_higgest)
        final.append(temp)

print(final)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值