大數據文摘出品
作者:李雷
有沒有也被拜年短信(大部分是羣發)搞得很焦慮?不回覆似乎顯得很沒有禮貌,一一回復又累心勞神。
有沒有既不浪費時間又能保持禮貌的辦法呢?
人生苦短,我用Python!
知乎博主“餘博倫”曾經在知乎上寫過用12行python代碼自動回覆拜年信息的文章
https://zhuanlan.zhihu.com/p/25034403
我們在前輩的基礎上,對代碼進行了部分修改,還根據最近的節奏,加入了“加班”和“女朋友”觸發彩蛋,讓你能夠自由增改自動回覆的內容。
手把手告訴你如何過個解放雙手的智能假期。在這裏還是要推薦下我自己建的裙前面是304再加上050最後是799,裙裏都是學Python開發的,如果你正在學習Python ,小編歡迎你加入,大家都是軟件開發黨,不定期分享乾貨(只有Python軟件開發相關的),包括我自己整理的一份2018最新的Python進階資料和高級開發教程,歡迎進階中和進想深入Python的小夥伴
windows環境
1.pip安裝
2.python安裝
3.pycharm
4.微信
實現:自動拜年回覆
1.Installing with get-pip.py
To install pip, securely download get-pip.py. [2]
PIP 官網首頁
https://pip.pypa.io/en/stable/installing/?spm=a2c4e.11154000.rtdmain.3.270f4283NCvVd9#installing-with-get-pip-py
get-pip.py 下載地址
https://bootstrap.pypa.io/get-pip.py?spm=a2c4e.11154000.rtdmain.4.270f4283mNOgtr&file=get-pip.py
Then run the following:
在python 加入環境變量;
CMD中 在get-pip.py的保存路徑下執行
2.get-pip.py
python的路徑 ,及 python下 pip的路徑都配置進入
3.成功 pip
pip 安裝 itchat 包的過程 01
pip 安裝 itchat 包的過程 02
成功
代碼
創建Python文件比如 newYear.py ,代碼內容如下:
"""
newYear.py test
"""
# coding: utf-8
import itchat, re
from itchat.content import *
import random
import json
import codecs
"""
Constants
"""
#REPLY = {'default': '祝您豬年大吉'}
REPLY = {'default': ''}
try:
with open("REPLY.json", 'r', encoding='utf-8') as load_f:
json_str = load_f.read()
print(json_str)
if json_str:
if json_str.startswith(u'\ufeff'):
json_str = json_str.encode('utf8')[3:].decode('utf8')
REPLY = json.loads(json_str)
except Exception as e:
print(e)
print("replies:", REPLY)
@itchat.msg_register([TEXT])
def text_reply(msg):
items = REPLY.items()
defFlag = True
for key, value in items:
#print(key, ",", value)
match = re.search(key, msg['Text'])
if match:
randomIdx = random.randint(0, len(REPLY[key])-1)
itchat.send(REPLY[key][randomIdx], msg['FromUserName'])
defFlag = False
break
if len(REPLY['default']) > 0 and defFlag:
itchat.send(REPLY['default'], msg['FromUserName'])
'''
@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])
def other_reply(msg):
itchat.send((REPLY['default'], msg['FromUserName']))
'''
itchat.auto_login(enableCmdQR=True, hotReload=True)
itchat.run()
運行 :
cmd 中
python newYear.py
屏幕出現二維碼 微信掃碼登陸,實現自動回覆
掃碼登錄
微信登陸成功
自動回覆效果
注意:
1、此回覆是使用了微信網頁端,即如果設置了自動回覆,電腦端微信自動退出。
2、自動回覆信息內容,都保存到了Json文件中,你可以用txt格式打開此文件夾,進行修改內容然後點擊保存,就可以只有定製新年彩蛋。
節省的時間就好好陪陪家人吧!最後,文摘菌祝大家新年快樂!