Created
October 18, 2012 15:28
-
-
Save coldfire-x/3912566 to your computer and use it in GitHub Desktop.
get weather info for chaoyao district Beijing, CN
This file contains hidden or 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 json | |
import httplib2 | |
# Chaoyang district, Beijing, CN | |
chaoyang = 101010300 | |
class ValidCityCode(object): | |
valid_code = { | |
101010300: 1, | |
} | |
def is_city_code_valid(self, city_code): | |
if self.valid_code.get(city_code, 0): | |
#print "your city code is ok" | |
return True | |
# make sure your city code is ok | |
def get_url_by_city_code(city_code): | |
return "http://www.weather.com.cn/data/sk/%s.html" % city_code | |
# get current weather info by city code | |
def weather(city_code): | |
vcd = ValidCityCode() | |
if not vcd.is_city_code_valid(city_code): | |
print "seem you feed me an invlid place, Mars?" | |
return False | |
httpAgent = httplib2.Http() | |
url = get_url_by_city_code(city_code) | |
response, content = httpAgent.request(url, 'GET') | |
if not response.status == 200: | |
print "Please try it again later" | |
content_json = json.loads(content)['weatherinfo'] | |
print u"城市: ", content_json['city'] | |
print u"温度: ", content_json['temp'], "C" | |
print u"风力: ", content_json['WS'] | |
if __name__ == '__main__': | |
weather(chaoyang) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment