从零开始学习--Python-wxpy7月1日

Python

                                                                                                                     ---小白121的记录笔记


微信定时性发短信

需要第三方 wxpy 这个库(点此)


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

bot = Bot() #初始化库

def get_material(): #获取素材
    url = 'http://open.iciba.com/dsapi/' #金山词霸的一个API
    html = requests.get(url) #获取源代码
    content = html.json()['content'] #过滤出content的内容
    note = html.json()['note'] #过滤出note的内容
    image = html.json()['fenxiang_img'] #过滤出fenxian_img的内容
    find = re.findall('/imag/(.*?).jpg', image)
    save = requests.get(image) #下载到save变量
    save_i = open('F:\\python测试\\data\\微信自动聊天image\\' + str(find[0]) + '.jpg','wb') #储存到本地
    save_i.write(save.content)
    save_i.close()
    return content, note, find


def wx_send():
    content, note, find = get_material()

    friend = bot.friends().search('121812')[0] #微信过滤出 名为 '121812' 的用户
    friend.send('%s'%content) #把内容发送到 '121812' 用户
    friend.send('%s'%note)

    friend.send_image('F:\\python测试\\data\\微信自动聊天image\\%s'%find[0] + '.jpg')
    t = Timer(86400, wx_send) #每 86400 秒(一天) 发送一次内容
    t.start() #开始计算时间



if __name__ == '__main__':
    wx_send()
                                                                                                                                                     ---改编自公众号botoo的文章

猜你喜欢

转载自blog.csdn.net/qq_42184699/article/details/80877273