基于Python的微信表情轰炸器
写在前面
- 最近闲的无聊,在吾爱上看到一个贴子-《为了防止女朋友怼我,我就先用python爬了3600个怼人表情包等她来战!》,然后就试试能不能写一个成品出来,于是就有了这篇帖子。
使用方法
- 在表情词里输入你想发送的表情,并输入轰炸次数,再点击开始轰炸,将鼠标移至微信对话框就好了。
注意:点击开始轰炸后,界面可能会卡一会儿,因为在爬取表情包。
困惑
- 红色提示部分原本是想做成倒计时的,结果在点击开始轰炸后,会先进行轰炸,再跳出倒计时提示,索性就让它一直显示了,如果你有好的解决办法请告诉我,源代码贴上。
源代码
from pynput.keyboard import Key,Controller as key_ct #键盘控制
from pynput.mouse import Button,Controller as mouse_ct #控制鼠标
import win32clipboard as w
from PIL import Image
from io import BytesIO
import time #导入时间包
import os
import requests
from lxml import etree
import tkinter as tk
import urllib.parse
#输入数据
def inputURL(str,count):
str=urllib.parse.quote(str)
url = "http://www.doutula.com/search?type=photo&more=1&keyword=" + str +"&page="
# print(url+count)
get_images(url,int(count))
# def foo():
# global sec
# clock=labelText.after(1000,foo) #延迟调用foo,每1000毫秒一次
# labelText["fg"]="white"
# labelText["bg"] = "red"
# if sec==0: #如果倒计时为零时
# labelText.after_cancel(clock) #取消after时钟函数
# labelText.place_forget()
# sec = 5
# else:
# labelText.place(x=150, y=110)
# labelText['text']="你还有"+str(sec)+"秒点击微信聊天对话框!!!" #设置按钮显示文字倒计时
# sec=sec-1 #倒计时
#提醒
# def countDown(entryInput1,entryInput2):
# labelText.after(1000,foo) #每1000毫秒调用一次foo
def initGui():
win.title("微信表情轰炸器")
#设置窗口大小
win.geometry('500x150')
#设置窗口是否可变长、宽,True:可变,False:不可变
win.resizable(width=False, height=False)
#输入窗口
tk.Label(win, text='请输入表情词:').place(x=50, y=30)
tk.Label(win, text='请输入轰炸次数:').place(x=260, y=30)
inputCode1 = tk.StringVar()
inputCode2 = tk.StringVar()
entryInput1 = tk.Entry(win, width=15, textvariable=inputCode1)
entryInput1.place(x=135, y=32)
entryInput2 = tk.Entry(win, width=10, textvariable=inputCode2)
entryInput2.place(x=360, y=32)
labelText = tk.Label(win,fg="white",bg="red",text="点击开始轰炸后,你有5秒的时间将鼠标移至微信对话框!!!")
labelText.place(x=100, y=110)
tk.Button(win, text='开始轰炸', width=6, command=lambda: [inputURL(entryInput1.get(),entryInput2.get())]).place(x=230, y=70)
#窗口置顶
win.wm_attributes('-topmost',True)
win.mainloop()
return
def mouse_click():
mouse=mouse_ct()
mouse.press(Button.left) #按压鼠标左键
mouse.release(Button.left) #松开鼠标左键
#图片连发
def send_images():
time.sleep(3)
keyboard=key_ct() #获取键盘权限
keyboard.press(Key.ctrl)
keyboard.press('v')
keyboard.release('v')
keyboard.release(Key.ctrl)
mouse_click() #鼠标左键点击
time.sleep(0.5) #休眠1秒
keyboard.press(Key.enter) #按下回车键
keyboard.release(Key.enter) #松开回车键
def InitMsg(msg):
img = Image.open(msg)
f=BytesIO() # 当然保存成文件再读取也是可以的,只是这样更快
img.convert('RGB').save(f,'BMP') # 转换成位图
data=f.getvalue()[14:] # 至于为啥要舍去文件头的14个字节看百度
f.close()
# 打开剪贴板
w.OpenClipboard()
# 清空剪贴板
w.EmptyClipboard()
# 设置剪贴板内容
w.SetClipboardData(w.CF_DIB, data)
# 关闭剪贴板
w.CloseClipboard()
def get_images(url,count):
for i in range(1,30):
url = url + str(i)
headers ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63 "}
res = requests.get(url,headers = headers).text
res_xpath = etree.HTML(res)
# 用xpath提取表情包的具体网址
bqb_urls = res_xpath.xpath('//*[@id="search-result-page"]/div/div/div[2]/div/div[1]/div[1]/div//img/@data-original')
for bqb_url in bqb_urls:
tempImg = BytesIO(requests.get(bqb_url).content)
InitMsg(tempImg)
send_images()
count= count-1
if count == 0:
break
if count == 0:
break
if __name__=='__main__':
#窗口
win = tk.Tk()
initGui()
体验地址
|