1. 程式人生 > >Python爬取天氣預報

Python爬取天氣預報

exc res http tee parser ror .cn date req

將持續更新……

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爬取天氣預報