需要下载的package
打开微信,打开要发送的好友聊天窗口即可。
# -*- coding: utf-8 -*-
# @Time : 2020/11/8 21:33
# @Author : AWAYASAWAY
# @File : 1.py
# @IDE : PyCharm
import win32gui
import win32api
import win32con
import time
import requests
from threading import Timer
import win32clipboard as w
def get_news():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 Edg/86.0.622.63'
}
url = 'http://open.iciba.com/dsapi'
response = requests.get(url, headers=headers)
content = response.json()['content']
note = response.json()['note']
return content, note
def getClipBoardText():
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
return d
def setClipBoardText(data):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardText(data)
w.CloseClipboard()
def ctrlV():
win32api.keybd_event(17, 0, 0, 0) # ctrl键位码是17
win32api.keybd_event(86, 0, 0, 0) # v键位码是86
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
def altS():
win32api.keybd_event(18, 0, 0, 0) # Alt键位码
win32api.keybd_event(83, 0, 0, 0) # s键位码
win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
weixin = win32gui.FindWindow(None, "微信")
win32gui.SetForegroundWindow(weixin)
left, top, right, bottom = win32gui.GetWindowRect(weixin)
print(left, top, right, bottom)
def send_news():
contents = get_news()
theWordYouWantToSay = contents
# for i in range(0, 2):
click_x = 732 - 423 + left + 80
# click_x 和clilck_y点到输入框
click_y = bottom - (918 - 775) + 60
win32api.SetCursorPos([click_x, click_y])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
setClipBoardText(u'Hello, Miao~miao' + '\n' + theWordYouWantToSay[0] + '\n' + theWordYouWantToSay[1]) # 自行修改好友名字
ctrlV()
win32api.keybd_event(13, 0, 0, 0) # 13 enter
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.5)
# t = Timer(86400, send_news) # 24h
# t = Timer(3, send_news) # 3s 设置每时间发送,不建议,亲测会被好友删除
# t.start()
if __name__ == '__main__':
# send_news()
# 设置最大运行次数,防止被删好友
runNums = 3
for i in range(runNums):
send_news()
如需对本代码进行修改和改进,建议先对文件助手发送。否则后果自负。