【python练手】获取城市天气质量
#!/usr/bin/python # -*- coding: utf-8 -*- # get city pm2.5 and ranking # python2.7 import sys import urllib2 # import threading from time import ctime from bs4 import BeautifulSoup def get_pm25(cityname): site = 'http://www.pm25.com/' + cityname + '.html' html = urllib2.urlopen(site) soup = BeautifulSoup(html,'html.parser') city = soup.find(class_ = 'bi_loaction_city') # city_name aqi = soup.find("a", {"class", "bi_aqiarea_num"}) # AQI index quality = soup.select(".bi_aqiarea_right span") # area quality result = soup.find("div", class_ = 'bi_aqiarea_bottom') # air description # print city.text print city.text + u'AQI Index: ' + aqi.text + u'\nAir quality: ' + quality[0].text + result.text print '*'*20 + ctime() + '*'*20 if __name__ == '__main__': #get_pm25('shenzhen') if 1 == len(sys.argv): print "usage: ./get_pm25.py beijing guangzhou ..." for i in range(1, len(sys.argv)): get_pm25(sys.argv[i])
执行结果: