python實現定時發送消息


#!/usr/bin/env python
#-*- coding:utf-8 -*- @Author : wujf @Time:2018/8/21 15:59
# 自動發送消息
from threading import Timer
from wxpy import *
import requests
import urllib2,re,urllib,requests
#bot = Bot(console_qr=2,cache_path='botoo.pk1) #把consol_qr=2去掉,二維碼是當做圖片彈出來,否則則是以像素的方式打印出來,后面的參數是熱登錄,
bot = Bot(cache_path=False) # 連接微信,會出現一個登陸微信的二維碼
#bot.self.send(u'測試') # 給自己發送消息

def get_news():
# 這里讀取今日糍粑
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
contents = r.json()['content']
translation = r.json()['translation']
return contents,translation



def send_news():
try:
my_friend = bot.friends().search(u'鄒唯')[0]
my_friend.send(u'今天又到了我給你發問候的時候,嘻嘻嘻\n')
my_friend.send(get_news()[0])
my_friend.send(get_news()[1][13:])
my_friend.send(u'晚安')
# t = Timer(4, send_news) # 這里是一天發送一次,86400s = 24h
# t.start()
except:
my_friend = bot.friends().search(u'風吹過的街道')[0] # 這里是你的微信昵稱 (加上u的作用是對后面的字符串進行unicode編碼。unicode是書寫國際文本的標准方法)
my_friend.send(u'您的消息發送失敗了')

if __name__ == '__main__':
send_news()

bot.join()


最后利用linux的定時任務實現定時

 


















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM