Python爬取天气预报
Posted 南云之苑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python爬取天气预报相关的知识,希望对你有一定的参考价值。
将持续更新……
1.实现爬取一天的天气预报
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
resp=urlopen(‘http://www.weather.com.cn/weather/101270101.shtml‘)
soup=BeautifulSoup(resp,‘html.parser‘)
tagDate=soup.find(‘ul‘, class_="t clearfix")
dates=tagDate.h1.string
tagToday=soup.find(‘p‘, class_="tem")
try:
temperatureHigh=tagToday.span.string
except AttributeError as e:
temperatureHigh=tagToday.find_next(‘p‘, class_="tem").span.string
temperatureLow=tagToday.i.string
weather=soup.find(‘p‘, class_="wea").string
tagWind=soup.find(‘p‘,class_="win")
winL=tagWind.i.string
print(‘今天是:‘+dates)
print(‘风级:‘+winL)
print(‘最低温度:‘+temperatureLow)
print(‘最高温度:‘+temperatureHigh)
print(‘天气:‘+weather)
2.爬取7天的天气预报
以上是关于Python爬取天气预报的主要内容,如果未能解决你的问题,请参考以下文章