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()