Created
February 26, 2014 07:18
-
-
Save Zuckonit/9225116 to your computer and use it in GitHub Desktop.
get the pm2.5 of special city
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
import urllib | |
import sys | |
import re | |
def pager(city, fmt='utf-8'): | |
url = 'http://www.chapm25.com/city/{0}.html'.format(city) | |
page = urllib.urlopen(url).read() | |
return page.decode(fmt, 'ignore') | |
def parser(page, regex): | |
return regex.findall(page) | |
def pm25(city): | |
page = pager(city) | |
regex = re.compile(r'<div\s+class="span8\s+pmtips">.*?cityid=.*?>(?P<pm>.+?)[ <].*?\((.*?)\)', re.DOTALL) | |
try: | |
p = parser(page, regex)[0] | |
pm = p[0] | |
sort = p[1] | |
print '%-10s %-5s%-30s' % (city, pm, sort) | |
except IndexError: | |
print 'no pm2.5 data of such city' | |
if len(sys.argv) > 1: | |
city = sys.argv[1] | |
else: | |
city = 'chengdu' | |
pm25(city) |
Author
Zuckonit
commented
Feb 26, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment