zoukankan      html  css  js  c++  java
  • 微信,爬取每日一句,发送至多人,多个群

    Timer(5, send_news) 

    每日一句,发送至多人,多个群

     1 # -*- coding: utf-8 -*-
     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         my0_friend = bot.friends().search(u'王琳杰')[0]    #你朋友的微信名称,不是备注,也不是微信帐号。
    27         my1_friend = bot.friends().search(u'浮生若梦')[0] 
    28         #my2_friend = bot.friends().search(u'e.g.')[0] 
    29 
    30         my0_groups = bot.groups().search(u'聊天机器人测试')[0]    #你群的微信名称。
    31         my1_groups = bot.groups().search(u'测试')[0] 
    32 
    33         my0_friend.send(get_news1()[0])
    34         #my_friend.send(get_news1()[1])
    35         #my_friend.send(get_news1()[2])
    36         #my_friend.send(get_news1()[1][5:])
    37         my1_friend.send(get_news1()[0])
    38         #my2_friend.send(get_news1()[0])
    39 
    40         my0_groups.send(get_news1()[0])
    41         my1_groups.send(get_news1()[0])
    42         #t = Timer(86400, send_news) #每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧
    43         t = Timer(5, send_news) 
    44         t.start()
    45     except:
    46         print(u"今天消息发送失败了")
    47 
    48 '''
    49 schedule.every().day.at("12:30").do(send_news) #规定每天12:30执行job()函数
    50 while True:
    51     schedule.run_pending()#确保schedule一直运行
    52     time.sleep(1)
    53 '''
    54 
    55 if __name__ == "__main__":
    56     send_news()
    57     #print(get_news1()[0])
    58     #print(get_news1()[1][5:])
  • 相关阅读:
    Extjs renderer函数
    孩子,教育,钱
    《新概念英语》的学习方法
    英语,想说爱你爱的太晚
    window.open模拟表单POST提交
    Extjs 解决grid分页bug问题
    Extjs 判断对象是非为null或者为空字符串
    linux shell 删除满足正则表达式的文件
    OpenCV 生成矩形mask
    测试Kaggle kernel commit 是否会删除以前的output
  • 原文地址:https://www.cnblogs.com/wanglinjie/p/9291658.html
Copyright © 2011-2022 走看看