2025年3月31日 星期一 乙巳(蛇)年 正月初一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

使用pyautogui自动在某网站投票的脚本

时间:08-22来源:作者:点击数:15

网页自动投票:

  • # !/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # Author:Hiuhung Wan
  • import re
  • import pyperclip
  • import pyautogui as py
  • from time import sleep
  • def select(line):
  • '''
  • 勾选要投票的行。
  • 第一行打勾的处的坐标是是:1100,93,每一行的行距是73
  • :param line:行号
  • :return:None
  • '''
  • x = 1100
  • y = 93 + (line - 1) * 73
  • py.click(x, y)
  • sleep(0.3)
  • def open_url(url):
  • '''
  • 打开网页
  • :param url:网址
  • :return:None
  • '''
  • py.rightClick(x0 + 16, y0 + 16)
  • sleep(1)
  • # 无痕模式
  • py.click(x0, y0 - 100)
  • sleep(1)
  • # 按F6,把光标切换到地址栏
  • py.press("f6")
  • sleep(0.2)
  • # 输入url
  • py.typewrite(url)
  • sleep(0.2)
  • py.press("enter")
  • sleep(5)
  • def get_vote_num():
  • '''
  • 获取目前最新票数
  • :return:目前最新票数的列表
  • '''
  • # 点击到界面里
  • py.click(int(screen_x / 2 - 220), int(screen_y / 2))
  • sleep(0.1)
  • # 鼠标向下滚400,滚到最底部
  • py.scroll(-400)
  • sleep(0.5)
  • # 全选网页文字
  • py.hotkey('ctrl', 'a')
  • # 复制
  • py.hotkey('ctrl', 'c')
  • sleep(0.1)
  • py.click()
  • # 读取粘贴板信息
  • str0 = pyperclip.paste()
  • # 去掉回车
  • str0 = str0.replace('\n', '')
  • # print(str0)
  • res = re.findall(r'\d+', str0)
  • # 用于存放大于100的数字
  • list_vote = []
  • for i in res:
  • j = int(i)
  • if j > 100:
  • list_vote.append(j)
  • # 把第一个2018删掉
  • del list_vote[0]
  • print("票数分别是:", list_vote)
  • return list_vote
  • def get_line_num_of_voting(list_vote):
  • '''
  • 传入全部人员的票数,得到要进行投票的序号(行号)
  • :param list_vote:目前最新票数的列表
  • :return:要进行投票的序号(行号)
  • '''
  • # 复制一份
  • list_vote_bak = list_vote[:]
  • # 去掉自己的票,刚好位于最后
  • list_vote_bak.pop(-1)
  • list_vote_bak.sort()
  • # 票数最少的前5名
  • list_vote_bak = list_vote_bak[:5]
  • # print(list1)
  • # 存放要选取的公司位于第几行
  • list_line_num = []
  • for i in list_vote_bak:
  • if i in list_vote:
  • j = list_vote.index(i)
  • list_vote[j] = 0
  • list_line_num.append(j + 1)
  • list_line_num.sort()
  • print("最少票行号:", list_line_num)
  • # 把自己加上
  • list_line_num.append(12)
  • return list_line_num
  • def voting(list_line_num):
  • '''
  • 投票
  • :param list_line_num: 要进行投票的序号(行号)
  • :return: None
  • '''
  • # 勾选
  • for i in list_line_num:
  • select(i)
  • # 提交投票
  • py.click(980, 1005)
  • sleep(1)
  • # 关闭窗口
  • py.click(1900, 13)
  • sleep(1)
  • if __name__ == '__main__':
  • url = 'https://dwz.cn/ymfarMUf'
  • # 屏幕分辨率,如1920*1080
  • screen_x, screen_y = py.size()
  • # 在屏幕底端(任务栏)找到浏览器图标
  • x0 = 255
  • y0 = 1047
  • for i in range(2):
  • open_url(url)
  • voting(get_line_num_of_voting(get_vote_num()))

不到15秒,可以投完一轮

  • 票数分别是: [744, 744, 743, 744, 742, 743, 771, 742, 742, 743, 742, 1022]
  • 最少票行号: [3, 5, 8, 9, 11]
  • 票数分别是: [744, 744, 744, 744, 743, 743, 771, 743, 743, 743, 743, 1023]
  • 最少票行号: [5, 6, 8, 9, 10]

今天刷票的时候,发现偶尔会因为5秒还没打开完网页而发生异常,所以加入了异常处理,

还有就是每次投完票后,检查是否投票成功。

最后说一下,x0,y0需要根据自己电脑设置

  • # !/usr/bin/env python
  • # -*- coding:utf-8 -*-
  • # Author:Hiuhung Wan
  • import re
  • import pyperclip
  • import pyautogui as py
  • from time import sleep
  • def select(line):
  • '''
  • 勾选要投票的行。
  • 第一行打勾的处的坐标是是:1100,93,每一行的行距是73
  • :param line:行号
  • :return:None
  • '''
  • x = 1100
  • y = 93 + (line - 1) * 73
  • py.click(x, y)
  • sleep(0.3)
  • def open_url(url):
  • '''
  • 打开网页
  • :param url:网址
  • :return:None
  • '''
  • py.rightClick(x0 + 16, y0 + 16)
  • sleep(1)
  • # 无痕模式
  • py.click(x0, y0 - 100)
  • sleep(1)
  • # 按F6,把光标切换到地址栏
  • py.press("f6")
  • sleep(0.2)
  • # 输入url
  • py.typewrite(url)
  • sleep(0.2)
  • py.press("enter")
  • sleep(5)
  • def get_vote_num():
  • '''
  • 获取目前最新票数
  • :return:目前最新票数的列表
  • '''
  • # 点击到界面里
  • py.click(int(screen_x / 2 - 220), int(screen_y / 2))
  • sleep(0.1)
  • # 鼠标向下滚400,滚到最底部
  • py.scroll(-400)
  • sleep(0.5)
  • # 全选网页文字
  • py.hotkey('ctrl', 'a')
  • # 复制
  • py.hotkey('ctrl', 'c')
  • sleep(0.1)
  • py.click()
  • # 读取粘贴板信息
  • str0 = pyperclip.paste()
  • # 去掉回车
  • str0 = str0.replace('\n', '')
  • # print(str0)
  • res = re.findall(r'\d+', str0)
  • # 用于存放大于100的数字
  • list_vote = []
  • for i in res:
  • j = int(i)
  • if j > 100:
  • list_vote.append(j)
  • # 把第一个2018删掉
  • del list_vote[0]
  • print("票数分别是:", list_vote)
  • return list_vote
  • def get_line_num_of_voting(list_vote):
  • '''
  • 传入全部人员的票数,得到要进行投票的序号(行号)
  • :param list_vote:目前最新票数的列表
  • :return:要进行投票的序号(行号)
  • '''
  • # 复制一份
  • list_vote_bak = list_vote[:]
  • # 去掉自己的票,刚好位于最后
  • list_vote_bak.pop(-1)
  • list_vote_bak.sort()
  • # 票数最少的前5名
  • list_vote_bak = list_vote_bak[:5]
  • # print(list1)
  • # 存放要选取的公司位于第几行
  • list_line_num = []
  • for i in list_vote_bak:
  • if i in list_vote:
  • j = list_vote.index(i)
  • list_vote[j] = 0
  • list_line_num.append(j + 1)
  • list_line_num.sort()
  • # print("最少票行号:", list_line_num)
  • # 把自己加上
  • list_line_num.append(12)
  • return list_line_num
  • def voting(list_line_num):
  • '''
  • 投票
  • :param list_line_num: 要进行投票的序号(行号)
  • :return: None
  • '''
  • # 勾选
  • for i in list_line_num:
  • select(i)
  • # 提交投票
  • py.click(980, 1005)
  • sleep(1)
  • # 判断有无投票成功
  • # 全选网页文字
  • py.hotkey('ctrl', 'a')
  • # 复制
  • py.hotkey('ctrl', 'c')
  • # 读取粘贴板信息
  • str0 = pyperclip.paste()
  • if '投票成功' not in str0:
  • # 投票不成功
  • print("投票不成功!!")
  • py.click(1900, 13)
  • sleep(1)
  • # else: # 投票成功
  • # pass
  • # 关闭窗口
  • py.click(1900, 13)
  • sleep(1)
  • if __name__ == '__main__':
  • url = 'https://dwz.cn/ymfarMUf'
  • # 屏幕分辨率,如1920*1080
  • screen_x, screen_y = py.size()
  • # 在屏幕底端(任务栏)找到浏览器图标
  • x0 = 157
  • y0 = 1047
  • for i in range(200):
  • try:
  • open_url(url)
  • voting(get_line_num_of_voting(get_vote_num()))
  • except:
  • print("发生了异常")
  • # 关闭窗口
  • py.click(1900, 13)
  • sleep(1)

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐