Skip to content

Instantly share code, notes, and snippets.

@Zuckonit
Created February 26, 2014 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zuckonit/9225116 to your computer and use it in GitHub Desktop.
Save Zuckonit/9225116 to your computer and use it in GitHub Desktop.
get the pm2.5 of special city
#!/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)
@Zuckonit
Copy link
Author

chengdu    172  排名 130/190                                     
shanghai   127  排名 64/190                     
beijing    518  排名 187/190 
hanzhou    518  排名 187/190
guangzhou  156  排名 87/190                     

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment