目录
一,工具
二,鼠标
1,实时显示鼠标位置
2,控制移动鼠标
3,控制点击鼠标
三,键盘
1,单键输入
2,组合键输入
四,实用demo
1,多网页依次点击固定位置的按钮
2,收集多个网页的链接
一,工具
pyautogui库
命令:pip3 install pyautogui==0.9.50
如果不指定版本,可能会在使用时报错: module 'pyscreeze' has no attribute 'locateOnWindow'
二,鼠标
1,实时显示鼠标位置
import pyautogui
while True:
print(pyautogui.position())
例如,我定位到一个关键的复制按钮的位置:Point(x=830, y=200)
2,控制移动鼠标
pyautogui.moveTo(x=830,y=200)
3,控制点击鼠标
pyautogui.click(1477,77,button='left')
pyautogui.click(830,200,button='left')
三,键盘
1,单键输入
pyautogui.press('a')
pyautogui.press('b')
2,组合键输入
pyautogui.hotkey('ctrl', 'v')
四,实用demo
1,多网页依次点击固定位置的按钮
假设现在打开了很多网页,每个网页的固定位置有个工具按钮,单击之后在固定位置有个复制按钮,我们要依次遍历每个网页,点击工具按钮和复制按钮,并把文本收集汇总起来。
V1版本:
import pyperclip
import time
import pyautogui
def clickFirstPage():
pyautogui.click(600, 25, button='left') #第1个网页页签的坐标
def closeFirstPage():
clickFirstPage()
pyautogui.hotkey('ctrl', 'w')
pageNum = 5 #网页的数量
clickFirstPage()
for i in range(0,pageNum):
pyautogui.click(600, 77, button='left') # url的坐标
pyautogui.hotkey('ctrl', 'c')
pyautogui.click(5400, 1400, button='left') # 记事本的坐标
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
pyautogui.click(1477,77,button='left') #工具按钮的坐标
time.sleep(0.1)
pyautogui.click(830,200,button='left') #复制按钮的坐标
time.sleep(0.1)
pyautogui.click(5400,1400,button='left') #记事本的坐标
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
closeFirstPage()
V2版本:
如果获取系统的剪切板,则不需要记事本,直接print
import pyperclip
import time
import pyautogui
def clickFirstPage():
pyautogui.click(20, 20, button='left') #第1个网页页签的坐标
def closeFirstPage():
clickFirstPage()
pyautogui.hotkey('ctrl', 'w')
stopPage = 'edge://favorites/?id=337'
clickFirstPage()
for i in range(0, 30):
pyautogui.click(600, 77, button='left') # url的坐标
pyautogui.hotkey('ctrl', 'c')
time.sleep(0.1)
txt = pyperclip.paste()
if txt == stopPage:
break
print(txt)
time.sleep(0.1)
pyautogui.click(1477,77,button='left') #工具按钮的坐标
time.sleep(0.1)
pyautogui.click(830,200,button='left') #复制按钮的坐标
time.sleep(0.1)
txt = pyperclip.paste()
print(txt)
closeFirstPage()
V3版本:
在V1的基础上,新增预处理功能,以及用固定页面作为停止条件的功能。
预处理:先把所有网页刷几遍,让资源加载出来。
import pyperclip
import time
import pyautogui
def clickFirstPage():
pyautogui.click(20, 20, button='left') #第1个网页页签的坐标
def closeFirstPage():
clickFirstPage()
pyautogui.hotkey('ctrl', 'w')
def copyOnePage(stopPage):
pyautogui.click(600, 77, button='left') # url的坐标
pyautogui.hotkey('ctrl', 'c')
time.sleep(0.05)
txt = pyperclip.paste()
if txt == stopPage:
return False
pyautogui.click(1477, 77, button='left') # 工具按钮的坐标
time.sleep(0.05)
pyautogui.click(830, 200, button='left') # 复制按钮的坐标
time.sleep(0.05)
txt2 = pyperclip.paste()
if txt2 != '' and 'm3u8' not in txt and txt !=txt2:
print(txt)
print(txt2)
closeFirstPage()
return True
def init():
clickFirstPage()
for i in range(0, 60):
pyautogui.hotkey('ctrl', 'tab')
time.sleep(0.3)
clickFirstPage()
stopPage = 'edge://favorites/?id=337'
init()
for i in range(0, 30):
if copyOnePage(stopPage) ==False:
break
V4版本:
在V3的基础上,新增对于没有加载出来的网页,额外等待的功能
import pyperclip
import time
import pyautogui
def clickFirstPage():
pyautogui.click(20, 20, button='left') #第1个网页页签的坐标
def closeFirstPage():
clickFirstPage()
pyautogui.hotkey('ctrl', 'w')
def copyOnePage(stopPage):
pyautogui.click(600, 77, button='left') # url的坐标
pyautogui.hotkey('ctrl', 'c')
time.sleep(0.05)
txt = pyperclip.paste()
if txt == stopPage:
return 0
pyautogui.click(1477, 77, button='left') # 工具按钮的坐标
time.sleep(0.05)
pyautogui.click(830, 200, button='left') # 复制按钮的坐标
time.sleep(0.05)
txt2 = pyperclip.paste()
if txt2 != '' and 'm3u8' not in txt and txt !=txt2:
print(txt)
print(txt2)
return 1
return 2
def init():
clickFirstPage()
for i in range(0, 60):
pyautogui.hotkey('ctrl', 'tab')
time.sleep(0.3)
clickFirstPage()
stopPage = 'edge://favorites/?id=337'
init()
for i in range(0, 30):
ret = copyOnePage(stopPage)
if ret == 0:
break
elif ret == 1:
closeFirstPage()
else:
time.sleep(1)
copyOnePage(stopPage)
closeFirstPage()
V5版本:
在V4的基础上,新增了监听按键功能,还新增了过滤url长度的功能。
import pyperclip
import time
import pyautogui
import pynput.keyboard
from threading import Thread
escFlag = False
def on_press(key):
# 监听按键
if str(key) == 'Key.esc':
global escFlag
escFlag = True
def clickFirstPage():
pyautogui.click(20, 20, button='left') #第1个网页页签的坐标
def closeFirstPage():
clickFirstPage()
pyautogui.hotkey('ctrl', 'w')
def copyOnePage(stopPage):
pyautogui.click(600, 77, button='left') # url的坐标
pyautogui.hotkey('ctrl', 'c')
time.sleep(0.1)
txt = pyperclip.paste()
if txt == stopPage:
return 0
pyautogui.click(1555, 77, button='left') # 工具按钮的坐标
time.sleep(0.3)
pyautogui.click(855, 200, button='left') # 复制按钮的坐标
time.sleep(0.15)
txt2 = pyperclip.paste()
if txt != '' and txt2 != '' and 'm3u8' not in txt and txt !=txt2 and len(txt)<100:
print(txt)
print(txt2)
return 1
return 2
def init():
clickFirstPage()
for i in range(0, 30):
pyautogui.hotkey('ctrl', 'tab')
time.sleep(0.5)
if escFlag:
return
clickFirstPage()
def downAllPage(stopPage):
init()
for i in range(0, 50):
if escFlag:
return
ret = copyOnePage(stopPage)
if ret == 0:
break
elif ret == 1:
closeFirstPage()
else:
time.sleep(1)
copyOnePage(stopPage)
closeFirstPage()
def main():
stopPage = 'edge://favorites/?id=283'
downAllPage(stopPage)
t = Thread(target=main)
with pynput.keyboard.Listener(on_press=on_press) as pklistener:
t.start()
t.join()
pklistener.join()
2,收集多个网页的链接
这里的例子是一个网页,里面有4行6列的24个窗口,每个窗口点进去都是一个新网页,但是在url里面搜不到这24个新网页的相关信息。
于是我们可以按照固定间隔,依次点击每一个窗口,然后再把url收集下来。
import pyperclip
import time
import pyautogui
import pynput.keyboard
from threading import Thread
escFlag = False
def on_press(key):
# 监听按键
if str(key) == 'Key.esc':
global escFlag
escFlag = True
def downOnePage(x,y):
pyautogui.click(x, y, button='left')
time.sleep(1)
pyautogui.click(600, 77, button='left') # url的坐标
time.sleep(0.5)
pyautogui.hotkey('ctrl', 'c')
time.sleep(0.5)
txt = pyperclip.paste()
time.sleep(0.5)
print(txt)
pyautogui.click(40, 70, button='left') #返回
time.sleep(0.5)
def downAllPage():
for x in range(0, 6):
if escFlag:
return
downOnePage(x*300+200,600)
pyautogui.click(1777, 222, button='left')
pyautogui.scroll(-550)
time.sleep(0.5)
def main():
downAllPage()
downAllPage()
downAllPage()
downAllPage()
def showPos():
while True:
print(pyautogui.position())
t = Thread(target=main)
with pynput.keyboard.Listener(on_press=on_press) as pklistener:
t.start()
t.join()
pklistener.join()