向QQ好友发送消息通过Python语句:
注意点(提前打开聊天窗口、设置代码暂停时间(不认大量数据由于发送频繁QQ会提示失败))
两个代码任选其一
import win32gui import win32con import win32clipboard import time class CSendQQMsg(): def __init__(self, friendName, msg): self.friendName = friendName self.msg=msg def setText(self):#把要发送的消息复制到剪贴板 win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, self.msg) win32clipboard.CloseClipboard() def sendmsg(self):#给好友发送消息 self.setText() hwndQQ = win32gui.FindWindow(None,self.friendName)#找到名字为'王三'的窗口 if hwndQQ == 0: print('未找到qq对话框') return win32gui.SendMessage(hwndQQ,win32con.WM_PASTE, 0, 0) win32gui.SendMessage(hwndQQ, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) if __name__ == '__main__': friendName = '狗莫莫' for i in range(1,100): msg="感受一下魔力吧。奥利给" qq = CSendQQMsg(friendName,msg) qq.sendmsg() time.sleep(0.3) # 强制等待时间 print("输出第:",i,"条")
import win32gui import win32con import win32clipboard as w import time class QQMessageSend: def send_message(self,msg): # 窗口名字,就是备注名 name = "狗儿子" # 将测试消息复制到剪切板中 w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT, msg) w.CloseClipboard() # 获取窗口句柄 handle = win32gui.FindWindow(None, name) # 填充消息 win32gui.SendMessage(handle, 770, 0, 0) # 回车发送消息 win32gui.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) def main(self): # 消息内容 msg = '收到。奥利给。啥回来打王者。好的' # 循环发送 for i in range(30): self.send_message(msg) time.sleep(0.3) # 强制等待10秒时间 if __name__ == '__main__': qq_message_send = QQMessageSend() qq_message_send.main()