目录
一,工具
二,鼠标
1,实时显示鼠标位置
2,控制移动鼠标
3,控制点击鼠标
三,键盘
1,单键输入
2,组合键输入
四,实用demo
demo1
demo2
demo3
demo4
pyautogui库
命令:pip3 install pyautogui==0.9.50
如果不指定版本,可能会在使用时报错: module 'pyscreeze' has no attribute 'locateOnWindow'
import pyautogui
while True:
print(pyautogui.position())
例如,我定位到一个关键的复制按钮的位置:Point(x=830, y=200)
pyautogui.moveTo(x=830,y=200)
pyautogui.click(1477,77,button='left')
pyautogui.click(830,200,button='left')
pyautogui.press('a')
pyautogui.press('b')
pyautogui.hotkey('ctrl', 'v')
假设现在打开了很多网页,每个网页的固定位置有个工具按钮,单击之后在固定位置有个复制按钮,我们要依次遍历每个网页,点击工具和复制按钮,并把文本收集汇总起来。
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()
场景和demo1相同。如果获取系统的剪切板,则不需要记事本,直接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()
在demo1的基础上,新增预处理功能,以及用固定页面作为停止条件的功能。
预处理:先把所有网页刷几遍,让资源加载出来。
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
在demo3的基础上,新增对于没有加载出来的网页,额外等待的功能
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()