1.Python——给你最爱的心灵鸡汤

1.安装wxpy requests

sudo pip install requests
sudo pip install wxpy

1.2 wxpy 登陆

 wxpy 使用起来非常简单,我们只需要创建一个bot 对象,程序运行后,会弹出二维码,扫描二维码后显示登陆成功。

 下述代码在登陆完成后,会向我们的文件传输助手发送一个“hello world!”。(每个程序都需要一个hello world)

from wxpy import *

bot = Bot()
bot.file_helper.send( 'hello world!' )

print ( "end" )


1.3 你的鸡汤


from __future__ import unicode_literals
import requests
import itchat
import time

def get_news ():
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 :
# 登陆你的微信账号,会弹出网页二维码,扫描即可
itchat.auto_login( hotReload = True )
# 获取你对应的好友备注,这里的小明我只是举个例子
# 改成你最心爱的人的名字。
my_friend = itchat.search_friends( name = u '小明' )
# 获取对应名称的一串数字
XiaoMing = my_friend[ 0 ][ "UserName" ]
# 获取金山字典的内容
message1 = str (get_news()[ 0 ])
content = str (get_news()[ 1 ][ 17 :])
message2 = str (content)
message3 = "来自你最爱的人"
# 发送消息
itchat.send(message1, toUserName = XiaoMing)
itchat.send(message2, toUserName = XiaoMing)
itchat.send(message3, toUserName = XiaoMing)
# 每86400秒(1天),发送1次,
# 不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,
# 很麻烦的一件事,就让他一直挂着吧
# t = time(86400, send_news())
# t.start()
except :
message4 = u "今天最爱你的人出现了 bug /(ㄒoㄒ)/~~"
itchat.send(message4, toUserName = XiaoMing)

def main ():
send_news()

if __name__ == '__main__' :
main()




猜你喜欢

转载自blog.csdn.net/ycy_dy/article/details/80344778