Python实现微信消息连续发送

我最近遇到了这样的一个问题,我的室友自从玩了原神以后就很少叫我打永劫无间了。有的人信誓旦旦地说着自己不是二刺螈,身体却很老实,在手机电脑ipad上都下载了原神还看起了b站攻略。

为警醒室友,本人利用前阵子花半小时学来的pyautogui模块进行了如下尝试:

操作如下:1、对微信搜索框进行截图并保存,后面用来比对定位,会返回搜索框的坐标位置用来输入好友昵称。 2、运行下面代码,点开微信窗口,时间一到自会进行全屏截图。3、代码会根据全屏截图与第一步中的图片进行对比,从而进行搜索和发送。

代码如下:保存完图片后可直接复制粘贴使用,我将上面的搜索框截图后保存名为xxx.jpg。

import os
import cv2
import time
import pyperclip #用于复制粘贴
import pyautogui

#从屏幕screen中找到source的位置坐标(找到微信搜索框的位置)
def findImg():
    im = pyautogui.screenshot()
    im.save('screen.png')
    screen = cv2.imread('./screen.png')
    mPicture= cv2.imread('./xxx.jpg')
    result = cv2.matchTemplate(mPicture,screen, cv2.TM_CCOEFF_NORMED)
    pos_start = cv2.minMaxLoc(result)[3] #获取最相似点相似坐标
    x = int(pos_start[0]) + int(mPicture.shape[1] / 2)
    y = int(pos_start[1]) + int(mPicture.shape[0] / 2)
    return x,y

#向搜索框中录入要查找的好友名称:name好友名称,x,y搜索框位置
def search_friend(x,y,name):
    pyautogui.click(x,y)
    time.sleep(1)
    #赋值好友名称
    pyperclip.copy(name)
    #粘贴复制内容
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(1)
    pyautogui.hotkey('enter')
    #向下移动100个像素定位到搜索到第一个好友位置点击
#   pyautogui.moveTo(x, y+80)
#   pyautogui.click(x, y+80)

    
#向好友发送消息
def send_msg(msg):
    pyperclip.copy(msg)
    pyautogui.hotkey('ctrl', 'v')
    pyautogui.hotkey('enter')

    
#主要程序
time.sleep(5)
x,y = findImg()
search_friend(x,y,'传输')
for i in range(10): 
    send_msg('你是二刺螈吗')
    time.sleep(1)

猜你喜欢

转载自blog.csdn.net/suic009/article/details/123329987