python天気照会(都市名を入力し、天気を出力)

1056 ワード

python天気を検索し、都市名を入力し、天気を出力
def get_whether(city_name):
	"""
	"""
	city_code_dict = { \
	'  ': '101010100', '  ': '101020100', \
	'  ': '101030100',  '  ': '101040100', \
	}
	if len(city_name) == 0:
		print "city name is null"
		sys.exit()
	if city_name not in city_code_dict:
		print "city not exists"
		sys.exit()
	postal_code = city_code_dict[city_name]
	if postal_code.isdigit() == False:
		print "input is not number!"
		sys.exit()
	url = "http://www.weather.com.cn/data/cityinfo/"+postal_code+".html"
	res = urllib2.urlopen(url)
	content = res.read()
	#print content
	result_dict = json.loads(content) #      json     
	item = result_dict.get('weatherinfo')  #      get  
	#print result_dict['weatherinfo']['city'] 
	print ("%s      :%s,    :%s,    :%s" %(item.get('city'), \
	item.get('weather'), item.get('temp2'), item.get('temp1')))