Python實現(xiàn)從百度API獲取天氣的方法
更新時間:2015年03月11日 09:26:39 作者:saintatgod
這篇文章主要介紹了Python實現(xiàn)從百度API獲取天氣的方法,實例分析了Python操作百度API的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)從百度API獲取天氣的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
__author__ = 'saint'
import os
import urllib.request
import urllib.parse
import json
class weather(object):
# 獲取城市代碼的uri
code_uri = "http://apistore.baidu.com/microservice/cityinfo?cityname="
# 獲取天氣信息的uri
weather_uri = "http://apistore.baidu.com/microservice/weather?cityid="
# 主處理邏輯
def mainHandle(self):
print("輸入你要查詢的天氣:")
city_name = input()
uri = self.code_uri + urllib.parse.quote(city_name)
ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
if ret['errNum'] != 0:
print(ret['retMsg'])
return False
else:
weather_uri = self.weather_uri + ret['retData']['cityCode']
data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
if data['errNum'] == 0:
ret_data = data['retData']
output = "城市名:" + city_name + "\r\n"
output += "更新時間:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
output += "天氣:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
output += "當(dāng)前溫度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
print(output)
return True
else:
print(data['errMsg'])
return False
if __name__ == "__main__":
weather = weather()
weather.mainHandle()
import os
import urllib.request
import urllib.parse
import json
class weather(object):
# 獲取城市代碼的uri
code_uri = "http://apistore.baidu.com/microservice/cityinfo?cityname="
# 獲取天氣信息的uri
weather_uri = "http://apistore.baidu.com/microservice/weather?cityid="
# 主處理邏輯
def mainHandle(self):
print("輸入你要查詢的天氣:")
city_name = input()
uri = self.code_uri + urllib.parse.quote(city_name)
ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
if ret['errNum'] != 0:
print(ret['retMsg'])
return False
else:
weather_uri = self.weather_uri + ret['retData']['cityCode']
data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
if data['errNum'] == 0:
ret_data = data['retData']
output = "城市名:" + city_name + "\r\n"
output += "更新時間:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
output += "天氣:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
output += "當(dāng)前溫度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
print(output)
return True
else:
print(data['errMsg'])
return False
if __name__ == "__main__":
weather = weather()
weather.mainHandle()
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Python字符串的encode與decode研究心得亂碼問題解決方法
為什么Python使用過程中會出現(xiàn)各式各樣的亂碼問題,明明是中文字符卻顯示成“\xe4\xb8\xad\xe6\x96\x87”的形式?2009-03-03Python網(wǎng)絡(luò)請求之Requests庫的高級功能運用
在這篇文章中我們將進(jìn)一步深入學(xué)習(xí)Requests庫的高級功能,包括處理重定向,設(shè)置超時,處理大文件以及錯誤和異常處理,需要的朋友可以參考下2023-08-08python實現(xiàn)批量nii文件轉(zhuǎn)換為png圖像
這篇文章主要介紹了python實現(xiàn)批量nii文件轉(zhuǎn)換為png圖像,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07