為什麼會 python 的人會被另一半愛的死去活來?
hi~
landis and gentlemen!!!

image
image.gif
long time 不見了!
這次,想說點有用的,就是我們在跟另一半相處的時候,經常會使用微信來進行感情傳遞。想想,如果我們能讓我們自己的微信有獨特的功能,比如,時不時微信自動“關心”一下對方,時不時自動提醒對方喝熱水,時不時自動查詢天氣狀況,提醒對方穿衣等等。
學習Python中有不明白推薦加入交流群
號:864573496群裡有志同道合的小夥伴
,互幫互助,群裡有不錯的視訊學習教程
和PDF!每晚8:00群裡直播
是不是很爽呢?
對於 Python 來說,這些都是小意思,那麼咱們就用Python來給我們的微信加點料,讓另一半對你愛的死去活來?

image
image.gif
看什麼看,開啟你的pycharm
新建一個love的python檔案

image
image.gif

image
image.gif
有一個牛逼的python模組叫itchat。
這個庫有多牛逼?它就是微信的神器!
快點匯入
之前沒有過itchat模組的就pip install一下。
接著一行程式碼就能登入你的微信,然後就可以用python控制了!
<pre>itchat.auto_login()
</pre>

image
image.gif
登入完之後
使用itchat的run方法
<pre>itchat.run()
</pre>
就可以讓python檢測對方發過來的訊息了。
<pre>@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing'])
def text_reply(msg):
itchat.send('%s: %s'%(msg['Type'], msg['Text']), msg['FromUserName'])
</pre>
在你的 Pycharm 試試看!
<pre>import itchat
@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing'])
def text_reply(msg):
itchat.send('%s: %s'%(msg['Type'], msg['Text']), msg['FromUserName'])
itchat.auto_login()
itchat.run()
</pre>

image
image.gif
右鍵執行 love,是不是有個二維碼?
用你的微信掃一掃,以網頁的方式登入!
登入完你就可以用你的程式碼
控制你的微信了!

image
image.gif
我們剛剛寫的程式碼,就是對方發給你啥,你就回復啥!
測試一下?
讓對方給你發個簡訊看看。

image
image.gif

image
image.gif
ok,沒毛病,測試成功。
那麼接下來,發揮你自己的想象力。
比如在每天晚上23點33分
跟對方說你們在一起多久了
很是愛對方
把對方感動的不要不要的
涉及到時間
所以我們要匯入datetime模組
<pre>from datetime import datetime
</pre>
接著我們在建立一個獲取在一起多少天的方法
<pre>def get_days():
now = datetime.now()
# 你們在一起的日子
start_day = datetime(2018, 11, 11)
days_num = (now - start_day).days
return days_num
</pre>
接著,我們在來個執行緒,當到了23點33分的時候
我們就傳送訊息給對方
<pre>def say_love():
while True:
now_time = datetime.now()
print(str(now_time.hour) + "---" + str(now_time.minute))
if now_time.hour == 23 and now_time.minute == 33:
message = 'oh~~親愛的,我們已經在一起' + str(get_days()) + '天了,我每天都記著呢,而且每天我都告訴你,我愛你哦!'
userinfo = itchat.search_friends( nickName='wistbean')
username = userinfo[0]['UserName']
itchat.send(message,toUserName=username)
time.sleep(60)
</pre>
執行一下程式碼
測試一下看看

image
image.gif

image
image.gif
恩,還不錯!
可以把程式放到伺服器上跑
那麼以後每天就自動給對方發有愛的資訊了
可以動動你的腦筋
比如
對方給你發訊息的時候
你可以去呼叫圖靈機器人的API
然後將返回的資訊傳送給對方
比如
你在玩遊戲或者看片的時候
可以設定個留言的方法
<pre>itchat.send('親愛的,我在學習哦,等會做完筆記我再回復你',toUserName=username)
</pre>
算了...
不能教太多
畢竟
愛情
還是要靠自己那顆心

image
image.gif
剛說到的程式碼
<pre>import itchat
from datetime import datetime
import threading
import time
@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing'])
def text_reply(msg):
itchat.send('%s: %s'%(msg['Type'], msg['Text']), msg['FromUserName'])
def get_days():
now = datetime.now()
# 你們在一起的日子
start_day = datetime(2018, 11, 11)
days_num = (now - start_day).days
return days_num
def say_love():
while True:
now_time = datetime.now()
print(str(now_time.hour) + "---" + str(now_time.minute))
if now_time.hour == 23 and now_time.minute == 33:
message = 'oh~~親愛的,我們已經在一起' + str(get_days()) + '天了,我每天都記著呢,而且每天我都告訴你,我愛你哦!'
userinfo = itchat.search_friends(nickName='wistbean')
username = userinfo[0]['UserName']
itchat.send(message,toUserName=username)
time.sleep(60)
itchat.auto_login()
t1 = threading.Thread(target=itchat.run)
t2 = threading.Thread(target=say_love)
t1.start()
t2.start()
t1.join()
t2.join()
</pre>
對了...
剛剛這個

image
image.gif
叼毛,
有點囂張,已經被我的小弟打死了。
案發現場如下

image
image.gif

image
image.gif