Py:利用pickle模块和API天气接口实现输入城市得到该城市的天气预报
生活随笔
收集整理的這篇文章主要介紹了
Py:利用pickle模块和API天气接口实现输入城市得到该城市的天气预报
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Py:利用pickle模塊和API天氣接口實現輸入城市得到該城市的天氣預報
?
?
目錄
輸出結果
實現代碼
?
?
輸出結果
?
?
?
實現代碼
# -*- coding: utf-8 -*- ''' Created on 2017年3月10日@author: niu ''' import urllib.request import json import pickle pickle_file=open("city_data.pkl","rb") city=pickle.load(pickle_file)cityname = input("你想查詢那個城市的天氣?") citycode="" try: citycode =city[cityname] except: print ("not Found") if citycode: try: url= "http://www.weather.com.cn/data/cityinfo/"+citycode+".html"#構造網址 content = urllib.request.urlopen(url).read()#讀取網頁源代碼 data =json.loads(content)#使用json庫將字符轉化為字典 #print type(data) #print (content) res =data["weatherinfo"]#獲取字典 str_temp=("%s :%s~%s")%(res["weather"],res["temp1"],res["temp2"])#格式化字符 print (str_temp)#輸出天氣信息 except: print ("Not Found!!" )?
利用子程序將城市數據載入
import pickle pickle_file=open("city_data.pkl","wb") pickle.dump(city,pickle_file) pickle_file.close()?
相關文章
Py:利用pickle模塊和API天氣接口實現輸入城市得到該城市的天氣預報
?
總結
以上是生活随笔為你收集整理的Py:利用pickle模块和API天气接口实现输入城市得到该城市的天气预报的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Py:递归求解汉诺塔,简单的几行编程可以
- 下一篇: Py之easygui:easygui的简