利用Python定时让微信发送信息

利用Python让微信发送实时信息

首先要在电脑中导入open CV的库
按快捷键win+R输入cmd
输入代码pip install python-opencv
安装成功如图所示(有可能出现一次未成功的情况,多下几次即可)
在这里插入图片描述
检测opencv是否安装成功
在终端输入python
再输入import cv2如果未出现 报错即说明导入成功

在这里插入图片描述
输入pip install wxpy导入所需要的模块
完整代码

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests

# 导入模块
from wxpy import *
# 初始化机器人,扫码登陆
bot = Bot()
# 搜索名称含有 "xxx"这里是自己的微信昵称
my_friend = bot.friends().search('xxx', sex=FEMALE, city="微信城市")[0]


 
bot = None
def get_news():
    #获取一个连接中的内容
    url = "http://open.iciba.com/dsapi/"
    r = requests.get(url)
    print(r.json())
    contents = r.json()['content']
    translation = r.json()['translation']
    return contents,translation
def login_wechat():
    global bot
    bot = Bot()
    # bot = Bot(console_qr=2,cache_path="botoo.pkl")#linux环境上使用
def send_news():
    if bot == None:
        login_wechat()

    try:
        my_friend = bot.friends().search(u'好友昵称')[0] #xxx表示微信昵称
        #my_friend.send(get_news()[0])
        #my_friend.send(get_news()[1][5:])
        my_friend.send(u"记得量体温哦!!")
        t = Timer(25200, send_news) #360是秒数
        t.start()
    except:
        print(u"失败!!")


if __name__ == "__main__":
    send_news()
    print(get_news()[0])

完成

猜你喜欢

转载自blog.csdn.net/xubuhui/article/details/88192346
今日推荐