Last active
August 23, 2017 15:37
-
-
Save imbyron/6055795 to your computer and use it in GitHub Desktop.
拙作:利用国家气象局的API查询天气的工具。
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/python | |
#coding:utf-8 | |
''' | |
@author:Byron | |
新浪微博:http://weibo.com/ziyuetk | |
''' | |
import urllib2, json | |
from city import city | |
yourcity = raw_input("你想查那个城市的天气?") | |
#测试yourcity变量是否可以返回你想要的值 | |
#print yourcity | |
url = "http://www.weather.com.cn/data/cityinfo/" + city[yourcity] + ".html" | |
#print url #同上 | |
response = urllib2.urlopen(url, timeout=10) | |
city_dict = response.read() | |
#此处打印出来是一个json格式的东西 | |
#print city_dict | |
#用Python的json库来解析获取到的网页json内容 | |
jsondata = json.JSONDecoder().decode(city_dict) | |
#定义几个变量用来储存解析出来的内容 | |
temp_low = jsondata['weatherinfo']['temp1'] | |
temp_high = jsondata['weatherinfo']['temp2'] | |
weather = jsondata['weatherinfo']['weather'] | |
print yourcity | |
print weather | |
print temp_low + "~" + temp_high |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sabrina-luo 左上角Download Gist