Python通过读取网站信息以及BeautifulSoup解析获取到pm2.5数据
大家好,我是一个新人,文笔不好请多多指教。前几个月入手了一个树莓派,又买了一套拓展板,就是为了能学习一下Python编程。最近青岛雾霾比较猖狂,每天上网上查雾霾信息实在是太麻烦了。所以我干脆打算用Python抓取网站信息再显示到我的拓展板上(我只会展示获取PM2.5的代码的)。结果还真成功了。这个程序需要你安装BeautifulSoup来解析网站。简单的看了一下,aqi,pm2.5啥啥都可以获取到。简直是神器!放一下获取Aqi的代码哈
import urllib2
import time
from bs4 import BeautifulSoup
import re
def getPM25(cityname):
site = 'http://www.pm25.com/city/' + cityname+'.html'
try:
html = urllib2.urlopen(site)
soup = BeautifulSoup(html,"html5lib")
city = soup.find("span",{"class":"city_name"})
aqi = soup.find("a",{"class":"cbol_aqi_num"})
pm25 = soup.find("span",{"class":"cbol_nongdu_num_1"})
pm25danwei = soup.find("span",{"class":"cbol_nongdu_num_2"})
quality = soup.find("span",{"class":re.compile('cbor_gauge_level\d$')})
result = soup.find("div",{"class":'cbor_tips'})
replacechar = re.compile("<.*?>")
space = re.compile(" ")
print city.string + " aqi = "+aqi.string
return int(aqi.string)
except Exception,e:
print "catched exception"
print e
return -1
if __name__ == '__main__':
while True:
aqi = getPM25('qingdao')#我是青岛人,所以是qingdao,假设你是北京人,你就输入beijing
if aqi == -1:
time.sleep(1)
continue
BeautifulSoup的确很强大,可结合selenium解析动态网页 楼主还是努力学学这种基础吧= = 反汇编就放一边吧= =
页:
[1]