提示: 爬取网站所在城市的天气信息,并返回城市名称、最高气温、最低气温、天气情况。
爬取网站
# http://www.weather.com.cn/weather/101100301.shtml
实现代码
【爬取所在城市的天气信息】
代码如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re, os
resp=urlopen('http://www.weather.com.cn/weather/101100301.shtml')
soup=BeautifulSoup(resp,'html.parser')
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
print('城市:'+'阳泉')
print('最低温度:'+temperatureLow)
print('最高温度:'+temperatureHigh)
print('天气:'+weather)