python擷取天氣預報
1. 注冊阿凡達資料
這個網站上(阿凡達資料)給出了很多資料的接口,這樣就省的你自己去爬,在這個網站上申請後,你在申請資料接口你會拿到一個appkey,這個appkey将來可以用來做一個接口參數。

你進入天氣預報,然後申請資料,最終可以得到一個appkey。并且在裡面還能看到接口的一些傳回json資訊。
2. 使用python擷取資料
2.1 導入子產品
本次需要導入import requests。
2.2 定義請求參數
url = 'http://api.avatardata.cn/Weather/Query'
appKey = '*********'
paramers = {'key':appKey, 'cityname':cityName}
content = requests.get(url,params=paramers)
jsContent = content.json()
這段代碼中url是固定的,appkey就是你自己的paakey,paramers中cityName就是你要查詢天氣的城市的名稱。然後通過requestas獲得了一個json串。
2.3 處理json
上面能夠得到了個json字元串,你隻需要根據官網上的介紹吧字元串處理成你想要的格式就行了。
比如你想沖json中拿到時間,那麼你可以這樣寫:
date = jsContent['result']['weather']['date']
這個書寫格式你可以根據json進行處理。
2.4 擷取天氣預報代碼
def getWeatherReport(cityName):
url = 'http://api.avatardata.cn/Weather/Query'
appKey = '*******'
paramers = {'key':appKey, 'cityname':cityName}
content = requests.get(url,params=paramers)
jsContent = content.json()
date = jsContent['result']['realtime']
jsWeather = jsContent['result']['weather'][0]
allContent = {}
allContent['AddressAndTime'] = "早安,^_^ \n今天是"+jsWeather['date'] + " " + u" 星期" + jsWeather['week']+"\n"
allContent['WeatherInfo']=processWeather(jsWeather['info']['day'])
jsLife = jsContent['result']['life']
allContent['LifeInfo'] = GetLifeInfo(jsLife['info'])+"\n"
return allContent['AddressAndTime']+allContent['WeatherInfo']+allContent['LifeInfo']
# 把天氣資訊進行處理
def processWeather(weatherInfo):
tianqi = "今天天氣 :"+ weatherInfo[1]+"\n"
wendu = weatherInfo[2]
# 我這裡把溫度做了下處理把數字變成動态文字了
try:
wenduValue = int(wendu)
if(abs(wenduValue-20)<=2):
wendu ="真舒服"
elif(wenduValue-20>2):
wendu = "熱"*((wenduValue-20)//3)
else:
wendu = "冷"*((20-wenduValue)//3)
wendu = "今天溫度:"+wendu+"\n"
except:
wendu = "今天溫度:"+wendu+"\n"
fengli = weatherInfo[4]
# 異常抛出的妙用 因為有些風力不是3-4級這樣類型,可能是微風這種,是以可能不能轉為整數
try:
fengli = int(fengli[0])
fengli ="今天的風力:"+"呼~"*fengli +"\n"
except:
fengli ="今天的風力:"+fengli +"\n"
return tianqi+wendu+fengli
# 處理每日穿衣資訊
def GetLifeInfo(info):
iIndex = 0
LifeInfo = {}
LifeInfo['chuanyi'] = u"穿 衣: " + info['chuanyi'][1]
LifeInfo['yundong'] = u"運 動: " + info['yundong'][1]
LifeInfo['ziwaixian'] = u"紫外線: " + info['ziwaixian'][1]
LifeInfo['ganmao'] = u"感冒: " + info['ganmao'][1]
strContent = ""
for item in LifeInfo.values():
strContent = strContent + item + "\n"
return strContent
3. 微信發送
怎麼使用微信聊天我在之前的一篇文章已經分享過了,是以不再累述。流程就是導入子產品–>定義Bot–>尋找好友–>發送内容(内容就是上面天氣預報處理後的一個字元串)。
如果你想定時發送就做一個線程,這樣每隔一天他就會自動發一次。
bot = Bot()
# linux執行登陸請調用下面的這句
#bot = Bot(console_qr=1)
def send_news():
print("begin----")
# 你朋友的微信名稱,不是備注,也不是微信帳号。
try:
my_friend = bot.friends().search('**')[0]
try:
contents = getWeatherReport("**")
my_friend.send(contents)
bot.file_helper.send(contents)
## 每86400秒(1天),發送1次,這裡加了一個随機,避免每天都同時
print("end------")
t = Timer(86400+60*random.randint(-6,6), send_news)
t.start()
except:
contents = "早安"
my_friend.send(contents)
bot.file_helper.send(contents)
except Exception as e:
# 向檔案傳輸助手發送消息
bot.file_helper.send('伺服器異常出問題了' + str(e))
參考:
https://blog.csdn.net/qq_28888837/article/details/80395616
https://blog.csdn.net/u011554976/article/details/80604004
https://blog.csdn.net/u012581604/article/details/77371543