使用python给女朋友自动发晚安

最近看到一篇微信推文,是关于python小程序的,就自己动手实现了一遍,在此记录一下:

from __future__ import unicode_literals

from threading import Timer

from wxpy import *

import requests

bot = Bot()

# bot = Bot(console_qr=2, cache_path='botoo.pkl') #这里的二维码是用像素的形式打印出来!,如果你在win环境上运行,替换为bot=Bot()

def get_news1():

    # 获取金山词霸每日一句,英文和翻译

    url = "http://open.iciba.com/dsapi/"

    r = requests.get(url)

    contents = r.json()['content']

    translation= r.json()['translation']

    return contents, translation

def send_news():

    try:

        my_friend = bot.friends().search(u"Fantasy")[0]    # 你朋友的微信昵称,不是备注,也不是微信帐号。

        my_friend.send(get_news1()[0])        # 此处获取的是get_news1()方法返回列表的第一部分内容

        my_friend.send(get_news1()[1][5:])     #词霸小编:野草遮不住太阳的光芒,……,即去掉词霸小编:
        my_friend.send(u"I Love You!")

        t = Timer(10, send_news)     # 每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧

        t.start()

    except:

        my_friend = bot.friends().search("Blueberry")[0]    # 你的微信昵称,不是微信帐号。

        my_friend.send(u"今天消息发送失败了")

if __name__ == "__main__":

    send_news()

首先需要安装一个wxpy包,使用pip install wxpy 命令即可,然后更改你的微信昵称和接收信息方的微信昵称。

猜你喜欢

转载自blog.csdn.net/sinat_29694963/article/details/80250969