• 自动化发送微信


    环境配置

    这里需要用到第三方库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()
    
  • 相关阅读:
    【cocos2d-js网络教程篇】cocos2d-js http网络请求
    COCOS2D
    Laravel5中的Session
    Laravel 下配置 Redis 让缓存、Session 各自使用不同的 Redis 数据库
    cocos-js Http方式网络请求
    Python语法31[module/package+import]
    cocos2d-js callFunc传参
    安装pygame for Python3.5
    阿里云vsftp安装和简单的配置
    Git代码行统计命令集
  • 原文地址:https://www.cnblogs.com/welan/p/9476955.html
Copyright © 2020-2023  润新知