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系統學習教程,
想要這些資料的可以關注私信小編“01”即可(免費分享哦)希望能對你有所幫助
閱讀更多 Python智禪 的文章