【点赞 收藏 关注 】仅供学习,仅供学习。
今天用Python 的 selenium模块,自动化过极验五子棋,先看视频
这个是手动验证的,需要输入指定数字才能开始验证,是一个while死循环,是为了方便展示
1、五子棋人机验证比上一次的哪个消消乐还要简单,哪个消消乐的图片的url全部都不一样,这个五子棋的图片的url都是一样的,又节约了时间和流量
2、这次的人机验证也没什么难度,看图说话
3、难度不大,也没什么好讲的,有些注释没写是因为屎山不需要注释,其次是因为我写代码时都是突然来的灵感,没来的及写,等回过头就忘了要写什么注释了,干脆就不写了,总之我都会,你们不会的可以自己研究一下,还不会就留言,等我给你慢慢讲
【附上代码】教程可能不是很清晰明了,有不懂可以留言或私信
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import re
import hashlib
def md5_encrypt(string):
md5 = hashlib.md5()
md5.update(string.encode('utf-8'))
return md5.hexdigest()
# 配置 Chrome 浏览器
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = "chrome/chrome.exe" # 指定 Chrome 浏览器的路径
chrome_options.add_argument("--disable-infobars") # 禁用浏览器中的"信息栏",避免干扰测试。
# 启动浏览器
driver = webdriver.Chrome(options=chrome_options)
# 打开 Google
driver.get("http://gt4.geetest.com/")
print('点击开始验证')
time.sleep(0.5)
# 点击配置
driver.find_element(By.XPATH, r'/html/body/div/div/div[4]/div[1]').click()
time.sleep(0.5)
# 点击五子棋验证
driver.find_element(By.XPATH, r'/html/body/div/div/div[4]/div[2]/div[2]/div[1]/div[2]/div[3]/div[6]').click()
time.sleep(0.5)
# 点击验证按钮
driver.find_element(By.XPATH, r'/html/body/div/div/div[2]/div[2]/form/div[3]/div[3]/div[1]/div[1]').click()
print('等待加载')
data = driver.page_source
# 字典格式的数据
list_chart1 = []
# 数据转哈希
list_chart2 = []
# 上下
for _y in range(5):
# 左右
for _x in range(5):
if re.findall(f'geetest_item-{_y}-{_x} geetest_itemimg geetest_isEmpty', data):
chart_url = ''
else:
chart_url = md5_encrypt(re.findall(f'geetest_item-{_y}-{_x}_.*?url\("(.*?)"\)', data)[0])
list_chart1 += [{f'{_y+1}-{_x+1}': chart_url}]
list_chart2 += [chart_url]
print(f'五子棋已排列完成:{list_chart1}')
# 去重
quchong = []
# 要查找的参数
chuang = ''
# 目标棋子 & 重点坐标
target = ['','']
print('开始检测五子棋并查找缺口!!!')
if list_chart2[4::4][:-1].count('') == 1 and len(set(list_chart2[4::4][:-1])) == 2:
quchong += ['1-5','2-4','3-3','4-2','5-1']
chuang = list(filter(None, set(list_chart2[4::4][:-1])))[0]
target[1] = f'{list_chart2[4::4][:-1].index("")+1}-{6-(list_chart2[4::4][:-1].index("")+1)}'
if list_chart2[::6].count('') == 1 and len(set(list_chart2[::6])) == 2:
quchong += ['1-1','2-2','3-3','4-4','5-5']
chuang = list(filter(None, set(list_chart2[::6])))[0]
target[1] = f'{list_chart2[::6].index("")+1}-{list_chart2[::6].index("")+1}'
for number in range(5):
number_1 = number * 5
if list_chart2[number_1:number_1+5].count('') == 1 and len(set(list_chart2[number_1:number_1+5])) == 2:
quchong += [f'{number_1 // 5 + 1}-{a+1}' for a in range(5)]
chuang = list(filter(None, set(list_chart2[number_1:number_1+5])))[0]
target[1] = list(list_chart1[number_1+list_chart2[number_1:number_1+5].index('')].keys())[0]
if list_chart2[number::5].count('') == 1 and len(set(list_chart2[number::5])) == 2:
quchong += [f'{a+1}-{number+1}' for a in range(5)]
chuang = list(filter(None,set(list_chart2[number::5])))[0]
target[1] = list(list_chart1[number+list_chart2[number::5].index('')*5].keys())[0]
if quchong:
for _ in list_chart1:
if list(_.keys())[0] not in quchong and list(_.values())[0] == chuang:
target[0] = list(_.keys())[0]
break
print(f'缺口方位:{target[0]} 目标棋子方位:{target[1]}')
print('准备移动 - 略等2秒,怕页面没加载出来!!!')
# 此处要加间隔时间,防止图片没有加载出来
time.sleep(2)
driver.find_element(By.XPATH,
rf'/html/body/div/div/div[2]/div[2]/form/div[3]/div[3]/div[1]/div[4]/div[1]/div[2]/div/div[2]/div[{target[0].split("-")[0]}]/div[{int(target[0].split("-")[1])*2}]').click()
time.sleep(0.1)
driver.find_element(By.XPATH,
rf'/html/body/div/div/div[2]/div[2]/form/div[3]/div[3]/div[1]/div[4]/div[1]/div[2]/div/div[2]/div[{target[1].split("-")[0]}]/div[{int(target[1].split("-")[1])*2}]').click()
print('验证成功!!!')
print('等待执行其他操作!!!\n\n')
time.sleep(10000)
# 关闭浏览器
driver.quit()