python实战1==用微信定时向朋友发送晚安

 

功能:用python代码实现向朋友定时发送消息

需要用到两个库:

pip install wxpy    #用来建立微信对象发送消息

pip install requests   #使用爬虫,抓取定向网页的内容

 

#-*- coding:utf-8 -*-
'''
功能:实现没每晚定时给朋友微信发送"晚安"等等!

'''

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

bot = Bot()

def get_news():
    '''获取金山词霸每日一句,英文和翻译'''
    url = "http://open.iciba.com/dsapi/"
    response = requests.get(url)
    content = response.json()['content']
    note = response.json()['note']
    return content, note

def send_news():
    try:
        content, note = get_news()
        # 你朋友的微信名称,不是备注,也不是微信账号。
        my_friend = bot.friends().search(u'张三')[0]
        my_friend.send(content)
        my_friend.send(note)
        my_friend.send(u'Have a good one!')
        t = Timer(86400, send_news)    # 定时器
        t.start()
    except:

        my_friend = bot.friends().search(u'李四')[0]
        my_friend.send(u'今天消息发送失败了')


if __name__ == '__main__':
    send_news()

 

posted @ 2021-07-12 17:04  宇宙刘  阅读(403)  评论(0)    收藏  举报