Python爬取天气预报


实现爬取一天的天气预报

非常简单的一个小爬虫,利用的也是基本的request、BeautifulSoup、re库,算是简单的上手一个小测试吧

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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM