1. 程式人生 > >python控制微信, 微信控制PC執行指令碼

python控制微信, 微信控制PC執行指令碼

import wxpy
import os, traceback, time
from selenium import webdriver


def wechat_login():
    '''
    監控微信
    :return:
    '''
    bot = wxpy.Bot()

    # 訊息接收監聽器
    @bot.register(bot.self, except_self=False)
    def print_others(msg):
        # 列印收到的訊息
        text = str(msg).strip()
        print(text)
        # 判斷關鍵指令並切片出所需要的附加資訊
        if 'start script' in text:
            text_pin = text[19:26]
            print(text_pin)
            # 呼叫函式控制瀏覽器
            ctrl_windows(text_pin)


def ctrl_windows(text_pin):
    global driver
    try:
        driver = webdriver.Chrome(r"F:\Google\Chrome\Application\chromedriver.exe")
        driver.maximize_window()
        driver.implicitly_wait(10)
        driver.get(url)

    finally:
        time.sleep(10)
        driver.quit()


wechat_login()
# 阻塞程序,使其一直在接收微信訊息的狀態
wxpy.embed()

參考資料: 

https://wxpy.readthedocs.io/zh/latest/index.html

https://www.cnblogs.com/jaycekon/archive/2017/07/17/WxpyRedis.html

https://blog.csdn.net/jiangop/article/details/77992716