zoukankan      html  css  js  c++  java
  • 用python实现微信定时发文

    pip install wxpy

    pip install schedule

     Timer实现定时

    wxpy是专门用于python处理个人用户微信的相关模块,这个模块可以查看朋友、查看群组、发信息、公众号操作等等,功能非常强大。

     1  
     2 from __future__ import unicode_literals
     3 from threading import Timer
     4 from wxpy import *
     5 import requests
     6 bot = None
     7 def get_news1():
     8     #获取金山词霸每日一句,英文和翻译
     9     url = "http://open.iciba.com/dsapi/"
    10     r = requests.get(url)
    11     print(r.json())
    12     contents = r.json()['content']
    13     note = r.json()['note']
    14     translation = r.json()['translation']
    15     return contents,note,translation
    16 def login_wechat():
    17     
    18     global bot
    19     bot = Bot()
    20     # bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux专用,像素二维码
    21  
    22 def send_news():
    23     if bot == None:
    24         login_wechat()
    25     try:
    26         my_friend = bot.friends().search(u'卿尘')[0]    #你朋友的微信名称,不是备注,也不是微信帐号。
    27         #my_friend = bot.groups().search(u'灯火阑珊处')[0]    #你群的微信名称,不是备注,也不是微信帐号。
    28         my_friend.send(get_news1()[0])
    29         my_friend.send(get_news1()[1])
    30         my_friend.send(get_news1()[2])
    31         #my_friend.send(get_news1()[1][5:])
    32         #t = Timer(86400, send_news) #每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧
    33         t = Timer(120, send_news) 
    34         t.start()
    35     except:
    36         print(u"今天消息发送失败了")
    37 if __name__ == "__main__":
    38     send_news()
    39     #print(get_news1()[0])
    40     #print(get_news1()[1][5:])
  • 相关阅读:
    软件工程学习总结
    第13次作业--邮箱的正则表达式
    第12次作业--你的生日
    第11次作业--字符串处理
    第10次作业
    找回感觉的练习
    基础网络流学习笔记
    卷积定理的证明
    快速xxx变换相关
    主席树相关
  • 原文地址:https://www.cnblogs.com/wanglinjie/p/9280914.html
Copyright © 2011-2022 走看看