python给qq好友发送消息
步骤
- 下载Pythoncharm
- 在pythoncharm里下载pipwin32的扩展(需要适应python版本,我python使用3.7版本)
代码
# 原理是先将需要发送的文本放到剪贴板中,然后将剪贴板内容发送到qq窗口 # 之后模拟按键发送enter键发送消息 import win32gui import win32con import win32clipboard as w import time def get_text(): """获取剪贴板文本""" w.OpenClipboard() d = w.GetClipboardData(win32con.CF_UNICODETEXT) w.CloseClipboard() return d def set_text(a_string): """设置剪贴板文本""" w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT, a_string) w.CloseClipboard() def send_qq(to_who, msg): """发送qq消息 to_who:qq消息接收人 msg:需要发送的消息 """ set_text(msg) # 将消息写到剪贴板 qq = win32gui.FindWindow(None, to_who) # 投递剪贴板消息到QQ窗体 win32gui.SendMessage(qq, 258, 22, 2080193) win32gui.SendMessage(qq, 770, 0, 0) # 模拟按下回车键 win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32gui.SendMessage(qq, win32con.WM_KEYUP, win32con.VK_RETURN, 0) to_who = 'xxx' # 需要qq打开,并且和xxx的消息开着 msg = '发送的消息' send_qq(to_who, msg) i = 100 # 执行次数 while i > 0: i -= 1 send_qq(to_who, msg) time.sleep(0.1)
相关推荐
ericxieforever 2020-09-29
yonggeno 2020-08-02
zk0 2020-06-07
viewerlin 2020-05-29
iamplane 2020-05-11
ruanjiankaifa00 2020-05-01
magic00 2020-04-15
ArLinux 2020-04-09
极客研习社 2020-03-15
xiaonao00 2020-02-16
linuxhh 2020-02-14
xiaonao00 2020-01-24
poplpsure 2020-01-21
ustbclearwang 2020-01-18
86206132 2020-01-09
孙雪峰 2019-10-24
tencentopen 2019-11-10
Norsaa 2019-11-09