首页 > 编程语言 > 详细

Python爬取天气预报

时间:2017-11-18 19:38:06      阅读:370      评论:0      收藏:0      [点我收藏+]

将持续更新……

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爬取天气预报

原文:http://www.cnblogs.com/yunlambert/p/7857543.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!