python beautifulsoup 抓取天气

先安装下面两个库

pip install beautifulsoup4
pip install html5lib

代码如下

from bs4 import BeautifulSoup
import urllib.request
url = "http://www.weather.com.cn/weather/101210101.shtml"
response = urllib.request.urlopen(url)
result = response.read().decode("utf-8")
#print(result)
soup = BeautifulSoup(result,'html5lib')
#print(soup)
today = soup.find_all("li", class_="sky skyid lv2 on")[0]
wea = today.find_all("p", class_="wea")[0]
tem = today.find_all("p", class_="tem")[0]
se = today.find_all("span", class_="SE")[1]
print ('日期:',today.h1.string)
print ('天气:%s %s'%(wea.get_text(),tem.get_text()))
print ('se:',se.get('title'))

运行结果:


python beautifulsoup 抓取天气_第1张图片
屏幕快照 2017-08-11 上午11.56.28.png

推荐一个linux命令行网站:https://rootopen.com

你可能感兴趣的:(python beautifulsoup 抓取天气)