美文网首页
python beautifulsoup 抓取天气

python beautifulsoup 抓取天气

作者: moodi | 来源:发表于2017-08-11 13:41 被阅读0次

先安装下面两个库

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'))

运行结果:


屏幕快照 2017-08-11 上午11.56.28.png

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

相关文章

网友评论

      本文标题:python beautifulsoup 抓取天气

      本文链接:https://www.haomeiwen.com/subject/dsilrxtx.html