python微信消息定时_python实现给微信指定好友定时发消息
python有很多有趣的庫,其中wxpy是連接微信的接口,具體可以查看官方文檔:http://wxpy.readthedocs.io/zh/latest/index.html。可以實現自動操作,wxpy 支持 Python 3.4-3.6,以及 2.7 版本。
一、安裝
win10環境,直接在cmd中,輸入
pip install wxpy
有時網絡不穩定,可能出現錯誤,重新執行操作嘗試一下。
二、簡單介紹
# 導入模塊
from wxpy import *
# 初始化機器人,掃碼登陸
bot = Bot()
# 搜索名稱含有 "游否" 的男性深圳好友
my_friend = bot.friends().search('游否', sex=MALE, city="深圳")[0]
三、詳細代碼
打開cmd,執行jupyter notebook,打開ipython環境,在打開的瀏覽器頁面中,新建一個python3的ipynb文件。
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
bot = None
def get_news():
#獲取一個連接中的內容
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
print(r.json())
contents = r.json()['content']
translation = r.json()['translation']
return contents,translation
def login_wechat():
global bot
bot = Bot()
# bot = Bot(console_qr=2,cache_path="botoo.pkl")#linux環境上使用
def send_news():
if bot == None:
login_wechat()
try:
my_friend = bot.friends().search(u'xxx')[0] #xxx表示微信昵稱
my_friend.send(get_news()[0])
my_friend.send(get_news()[1][5:])
my_friend.send(u"咦?我是自動人!!")
t = Timer(360, send_news) #360是秒數
t.start()
except:
print(u"失敗!!")
if __name__ == "__main__":
send_news()
print(get_news()[0])
然后按ctrl+enter鍵執行。
總結
以上是生活随笔為你收集整理的python微信消息定时_python实现给微信指定好友定时发消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: medusa 使用教程
- 下一篇: strtok()函数详解!