溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

基于Python實(shí)現(xiàn)定時(shí)自動(dòng)給微信好友發(fā)送天氣預(yù)報(bào)

發(fā)布時(shí)間:2020-09-10 10:47:19 來(lái)源:腳本之家 閱讀:259 作者:李聰你好 欄目:開(kāi)發(fā)技術(shù)

效果圖

基于Python實(shí)現(xiàn)定時(shí)自動(dòng)給微信好友發(fā)送天氣預(yù)報(bào)

from wxpyimport *
import requests
from datetimeimport datetime
import time
from apscheduler.schedulers.blockingimport BlockingScheduler#定時(shí)框架
bot = Bot(cache_path=True)
tuling = Tuling(api_key=你的api')#機(jī)器人api
def send_weather(location):
#準(zhǔn)備url地址
path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
url = path % location
response = requests.get(url)
result = response.json()
#如果城市錯(cuò)誤就按照濮陽(yáng)發(fā)送天氣
if result['error'] !=0:
location ='濮陽(yáng)'
url = path % location
response = requests.get(url)
result = response.json()
str0 = ('  早上好!這是今天的天氣預(yù)報(bào)!……機(jī)器人:PyChatBot\n')
results = result['results']
# 取出數(shù)據(jù)字典
data1 = results[0]
# 取出城市
city = data1['currentCity']
str1 ='  你的城市: %s\n' % city
# 取出pm2.5值
pm25 = data1['pm25']
str2 ='  Pm值  : %s\n' % pm25
# 將字符串轉(zhuǎn)換為整數(shù) 否則無(wú)法比較大小
if pm25 =='':
pm25 =0
pm25 =int(pm25)
# 通過(guò)pm2.5的值大小判斷污染指數(shù)
if 0 <= pm25 <35:
pollution ='優(yōu)'
elif 35 <= pm25 <75:
pollution ='良'
elif 75 <= pm25 <115:
pollution ='輕度污染'
elif 115 <= pm25 <150:
pollution ='中度污染'
elif 150 <= pm25 <250:
pollution ='重度污染'
elif pm25 >=250:
pollution ='嚴(yán)重污染'
str3 ='  污染指數(shù): %s\n' % pollution
result1 = results[0]
weather_data = result1['weather_data']
data = weather_data[0]
temperature_now = data['date']
str4 ='  當(dāng)前溫度: %s\n' % temperature_now
wind = data['wind']
str5 ='  風(fēng)向  : %s\n' % wind
weather = data['weather']
str6 ='  天氣  : %s\n' % weather
str7 ='  溫度  : %s\n' % data['temperature']
message = data1['index']
str8 ='  穿衣  : %s\n' % message[0]['des']
str9 ='  我很貼心: %s\n' % message[2]['des']
str10 ='  運(yùn)動(dòng)  : %s\n' % message[3]['des']
str11 ='  紫外線 : %s\n' % message[4]['des']
str = str0 + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11
return str
#好友列表
my_friends = []
my_friends = bot.friends()
my_friends.pop(0)
#發(fā)送函數(shù)
def send_message():
#給全體好友發(fā)送
for friendin my_friends:
friend.send(send_weather(friend.city))
#發(fā)送成功通知我
bot.file_helper.send(send_weather('濮陽(yáng)'))
bot.file_helper.send('發(fā)送完畢')
#定時(shí)器
print('star')
sched = BlockingScheduler()
sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=5,minute =30)
sched.start()

具體操作:

首先導(dǎo)入wxpy、圖靈機(jī)器人和定時(shí)器Apscheduler,定時(shí)器用來(lái)定時(shí)群發(fā)。

具體pip操作建議百度。

使用百度的一個(gè)天氣接口得到j(luò)son數(shù)據(jù)。

主要思路:

1.從wxpy獲取好友列表

2.創(chuàng)建定時(shí)器

3.定時(shí)器觸發(fā)函數(shù)

4.函數(shù)執(zhí)行,遍歷好友列表

5.好友對(duì)象執(zhí)行帶參函數(shù),參數(shù)為該好友城市

6.函數(shù)中請(qǐng)求百度天氣接口,得到該好友對(duì)應(yīng)天氣數(shù)據(jù),解析處理數(shù)據(jù),發(fā)送天氣信息,完成該對(duì)象發(fā)送。

7.遍歷結(jié)束,發(fā)送完畢

缺陷:打包為exe文件之后啟動(dòng)失敗,原因是定時(shí)器找不到trigger,要想解決需要查看Apscheduler相關(guān)資料。

解決方法:換一種定時(shí)器。

編譯器上正常執(zhí)行。

打包為exe之后,可以很方便發(fā)給別人使用。掃碼登錄后每天早上5:30會(huì)自動(dòng)給所有好友發(fā)送效果圖中的天氣預(yù)報(bào)。

PS:下面看下Python實(shí)現(xiàn)微信定時(shí)發(fā)送天氣預(yù)報(bào)

schedule實(shí)現(xiàn)定時(shí)

 import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from wxpy import *
import schedule
import time
bot=Bot(cache_path=True) #登陸網(wǎng)頁(yè)微信,并保存登陸狀態(tài)
def sendblogmsg(content):
  #搜索自己的好友,注意中文字符前需要+u
  my_friend = bot.friends().search(u'卿塵')[0]
  my_friend.send(content)
  #my_group = bot.groups().search(u'卿塵')[0]
  #my_group.send(content) #發(fā)送天氣預(yù)報(bào)
def job():
  resp=urlopen('http://www.weather.com.cn/weather/101010100.shtml')
  soup=BeautifulSoup(resp,'html.parser')
  tagToday=soup.find('p',class_="tem") #第一個(gè)包含class="tem"的p標(biāo)簽即為存放今天天氣數(shù)據(jù)的標(biāo)簽
  try:
    temperatureHigh=tagToday.span.string #有時(shí)候這個(gè)最高溫度是不顯示的,此時(shí)利用第二天的最高溫度代替。
  except AttributeError as e:
    temperatureHigh=tagToday.find_next('p',class_="tem").span.string #獲取第二天的最高溫度代替
  temperatureLow=tagToday.i.string #獲取最低溫度
  weather=soup.find('p',class_="wea").string #獲取天氣
  contents = '北京' + '\n' + '最高溫度:' + temperatureHigh + '\n' + '最低溫度:' + temperatureLow + '\n' + '天氣:' + weather 
    # result3 = '最低溫度:' + temperatureLow
  #print('最低溫度:' + temperatureLow)
  #print('最高溫度:' + temperatureHigh)
  # print('天氣:' + weather)
  sendblogmsg(contents)
#定時(shí)
schedule.every().day.at("19:20").do(job) #規(guī)定每天12:30執(zhí)行job()函數(shù)
while True:
  schedule.run_pending()#確保schedule一直運(yùn)行
  time.sleep(1)
bot.join() #保證上述代碼持續(xù)運(yùn)行

end

總結(jié)

以上所述是小編給大家介紹的基于Python實(shí)現(xiàn)定時(shí)自動(dòng)給微信好友發(fā)送天氣預(yù)報(bào),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI