参考http://t.csdn.cn/AjQIU,说是参考其实就是照搬过来的,留给自己当作一个记录。
一个pyautogui的B站点赞的小案例。
需要引入的库,这两个都是直接可以使用PyCharm自动下载的:
import pyautogui
import time
手动执行: pip install pyautogui pip install pillow 这里加不加入都可以,最好加入下,稳定点:
pyautogui.PAUSE = 1 # 执行pyautogui命令时,会停顿一秒
pyautogui.FAILSAFE = True # 鼠标移动到0,0坐标点的时候会强行停止
主要代码:
def dainzan():
time.sleep(0.5) # 等待0.5s
left, top, width, height = pyautogui.locateOnScreen('img/zan.png') # 寻找图片
center = pyautogui.center((left, top, width, height)) # 寻找图片中心位置
pyautogui.click(center) # 点击中心
print("点赞成功")
while True:
if pyautogui.locateOnScreen('img/zan.png'):
dainzan() # 调用点赞函数
else:
pyautogui.scroll(-500) # 本页没有图片,滚动鼠标
print("没有目标,滚动一下")
如果不想加一个img文件来存储图片,而是图片和dianzan.py放在一起。也可以将主要代码替换为如下:
def dainzan():
time.sleep(0.5) # 等待0.5s
left, top, width, height = pyautogui.locateOnScreen('zan.png') # 寻找图片
center = pyautogui.center((left, top, width, height)) # 寻找图片中心位置
pyautogui.click(center) # 点击中心
print("点赞成功")
while True:
if pyautogui.locateOnScreen('zan.png'):
dainzan() # 调用点赞函数
else:
pyautogui.scroll(-500) # 本页没有图片,滚动鼠标
print("没有目标,滚动一下")
实际演示:
使用的PyCharm版本:
python版本:
阿里云盘莫名其妙分享不了代码,算了以后再说吧。复制粘贴上面的代码也能用。
pyscreeze.PyScreezeException: The Pillow package is required to use this function.
这个是导入库有问题的报错,需要加一个pillow库:pip install pillow
Traceback (most recent call last):
File "E:\dianzan\dianzan.py", line 17, in <module>
if pyautogui.locateOnScreen('img/zan.png'):
File "E:\dianzan\venv\lib\site-packages\pyautogui\__init__.py", line 175, in wrapper
return wrappedFunction(*args, **kwargs)
File "E:\dianzan\venv\lib\site-packages\pyautogui\__init__.py", line 213, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
File "E:\dianzan\venv\lib\site-packages\pyscreeze\__init__.py", line 372, in locateOnScreen
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
File "E:\dianzan\venv\lib\site-packages\pyscreeze\__init__.py", line 144, in wrapper
raise PyScreezeException('The Pillow package is required to use this function.')
pyscreeze.PyScreezeException: The Pillow package is required to use this function.
这是强行停止后的报错:
Traceback (most recent call last):
File "E:\dianzan\dianzan.py", line 17, in <module>
if pyautogui.locateOnScreen('zan.png'):
File "D:\python\lib\site-packages\pyautogui\__init__.py", line 175, in wrapper
return wrappedFunction(*args, **kwargs)
File "D:\python\lib\site-packages\pyautogui\__init__.py", line 213, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
File "D:\python\lib\site-packages\pyscreeze\__init__.py", line 373, in locateOnScreen
retVal = locate(image, screenshotIm, **kwargs)
File "D:\python\lib\site-packages\pyscreeze\__init__.py", line 353, in locate
points = tuple(locateAll(needleImage, haystackImage, **kwargs))
File "D:\python\lib\site-packages\pyscreeze\__init__.py", line 318, in _locateAll_python
if needleImageData[searchy * needleWidth:(searchy+1) * needleWidth] != haystackImageData[haystackStart:haystackStart + needleWidth]:
KeyboardInterrupt