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)
用PYTHON获取7天最高温度
最新推荐文章于 2023-08-06 16:48:55 发布