Python實(shí)現(xiàn)從百度API獲取天氣的方法
更新時(shí)間:2015年03月11日 09:26:39 作者:saintatgod
這篇文章主要介紹了Python實(shí)現(xiàn)從百度API獲取天氣的方法,實(shí)例分析了Python操作百度API的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Python實(shí)現(xiàn)從百度API獲取天氣的方法。分享給大家供大家參考。具體實(shí)現(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 += "更新時(shí)間:" + 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 += "更新時(shí)間:" + 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()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python matplotlib畫圖庫(kù)學(xué)習(xí)繪制常用的圖
這篇文章主要為大家詳細(xì)介紹了python matplotlib畫圖庫(kù)學(xué)習(xí)繪制常用的圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python 保持登錄狀態(tài)進(jìn)行接口測(cè)試的方法示例
這篇文章主要介紹了Python 保持登錄狀態(tài)進(jìn)行接口測(cè)試的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-08-08
django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例
今天小編就為大家分享一篇django傳值給模板, 再用JS接收并進(jìn)行操作的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
解決python中畫圖時(shí)x,y軸名稱出現(xiàn)中文亂碼的問題
今天小編就為大家分享一篇解決python中畫圖時(shí)x,y軸名稱出現(xiàn)中文亂碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01

