日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python输入城市名称_python 查询天气(输入城市名,输出天气)

發布時間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python输入城市名称_python 查询天气(输入城市名,输出天气) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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')))

總結

以上是生活随笔為你收集整理的python输入城市名称_python 查询天气(输入城市名,输出天气)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。