zoukankan      html  css  js  c++  java
  • 自动化发送微信

    环境配置

    这里需要用到第三方库pillow, wxpy,requests,可以直接通过pip命令安装

    • pillow:在终端中显示登陆二维码,需要安装 pillow 模块
    • wxpy:实例化机器人对象
    • requests:用户抓取网页数据

    金山词霸开放api

    这里使用金山词霸开放api 入口
    金山词霸每日一句,数据格式为json

    {
    "content":"I love the broken sound in my body and the healingprocess.",
    "note":"u6211u559cu6b22u6211u8eabu4f53u7834u788eu7684u58f0u97f3u548cu4e0du65adu6108u5408u7684u8fc7u7a0bu3002",
    "fenxiang_img":"http://cdn.iciba.com/web/news/longweibo/imag/2018-08-14.jpg"
    }
    

    也可以使用其他接口获取数据,但是一般这种开放接口每天都会有最大访问次数。

    源码

    from threading import Timer
    from wxpy import *
    import requests
    
    class Wechat:
        receiver = ''  # 为接受者的微信昵称
        second_receiver = ''  # 第二接受者的昵称
        def __init__(self, receiver, second_receiver):
            self.receiver = receiver
            self.second_receiver = second_receiver
    
        def get_news(self):
            url = "http://open.iciba.com/dsapi/"  # 调用金山词霸每日一句开放接口
            r = requests.get(url)  # 获取网页数据,数据格式为json
            content = r.json()['content']
            note = r.json()['note']
            fenxiang_img = r.json()['fenxiang_img']
            return content, note, fenxiang_img
    
        def send_news(self):
            bot = Bot()  # 实例化机器人对象
            try:
                content = self.get_news()  # 获取发送消息内容
                my_friend = bot.friends().search(self.receiver)[0]  # 将第一个昵称为receiver的微信好友作为消息接受者
                my_friend.send(contents[0])  # 发送每日一句的content
                my_friend.send(contents[1])  # 发送note
                my_friend.send(contents[2])  # 发送图片
                # 每86400秒(1天),发送1次
                t = Timer(86400, self.send_news)
                t.start()
            except:
                my_friend = bot.friends().search(self.second_receiver)[0]  # 如果第一个接受者发送失败,则发送给第二个
                my_friend.send(u"今天消息发送失败了")  # 发送提示错误消息
    
    if __name__ == '__main__':
        wechat = Wechat('xxx', ‘xxx’)
        wechat.send_news()
    
  • 相关阅读:
    【转】配置BT5中文环境
    Jaspersoft iReport Designer 4.7.0 导出pdf 中文不显示的解决办法
    JS通过get、post向jsp传递中文出现乱码的问题的解决
    从 相机 或者相册 获取图片显示在ImageView 上
    简单几段代码实现窗口抖动
    android 塔防游戏汇总 及android 游戏开发索引
    android 音乐播放器汇总
    android Style应用
    android手机控制电脑源码
    【Android通过手势实现的缩放处理】
  • 原文地址:https://www.cnblogs.com/welan/p/9476955.html
Copyright © 2011-2022 走看看