一个简单的pyautoguiB站点赞的小例子

参考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
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值