Python+selenium破解拼图验证码的脚本
作者:我去热饭 时间:2023-11-22 05:24:05
实现思路
很多网站都有拼图验证码
1.首先要了解拼图验证码的生成原理
2.制定破解计划,考虑其可能性和成功率。
3.编写脚本
很多网站的拼图验证码都是直接借助第三方插件,也就是一类一种解法。
笔者遇到的这种拼图验证码实际上是多个小碎片经过重新组合成的一张整体,首先要在网站上抓取这种小碎片图片并下载到本地
我们先捋一捋大体思路:
获取所有碎片图片----找出他们的排列顺序逻辑-----找出他们中含有颜色深的真正位置的那个小碎块的序号-----根据每块碎片的宽度和上下和这个深色小块的序号算出距离----用selenium向右移动滑块到这个距离
核心代码
先导入必要的包:
# -*- coding:utf-8 -*-
import colorsys
import urllib,os,uuid,re,time
from PIL import Image
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
抓到并下载到本地
def create(locapath,fileName):
filePath=locapath+'/'+fileName
if not os.path.exists(filePath):
file=open(filePath,'a+')
file.close()
return filePath
def downloadImg():
list = openBrowser()
for i in range(2):
fileName = str(i) + '_test.jpg'
urllib.urlretrieve(list[i], create('/Users/zijiawang/Downloads/', fileName)) #下载到本地
time.sleep(3)
def openBrowser():
global wzj
wzj = webdriver.Firefox()
wzj.get('https://。。。。')
image1_url = wzj.find_elements_by_class_name('gt_cut_bg_slice')[0].get_attribute('style')
image1_url=image1_url[23:-38]
image2_url = wzj.find_elements_by_class_name('gt_cut_fullbg_slice')[0].get_attribute('style')
image2_url=image2_url[23:-38]
return [image1_url,image2_url]
分割并获取rgb颜色
ef getcolor(image):
list = [[x,y] for x in range(26) for y in range(2)]
listt = []
colors = []
for i in range(len(list)):
l = list[i][0]*12+12
w = list[i][1]*58+58
listt.append([12*0.25+i/2*12, 58*0.25+((i+10)%2)*58])
listt.append([12*0.25+i/2*12, 58*0.75+((i+10)%2)*58])
listt.append([12*0.75+i/2*12, 58*0.25+((i+10)%2)*58])
listt.append([12*0.75+i/2*12, 58*0.75+((i+10)%2)*58])
for i in range(len(listt)):
colors.append(image[listt[i][0],listt[i][1]])
return colors
找出不同的小碎片序号
def getDeferent():
deferent = []
for i in range(208):
if abs(color1[i][2] - color2[i][2]) >20:
print u'B差值: ',color1[i][2]-color2[i][2]
deferent.append(i)
# print ([i,abs(color1[i][0] - color2[i][0]),abs(color1[i][1] - color2[i][1]),abs(color1[i][2] - color2[i][2])])
return deferent
def getUPDOWN():
deferent = getDeferent()
xlist = []
ylist = []
for i in deferent:
if (i / 4) % 2 == 0:
xlist.append(i)
else:
ylist.append(i)
uplist = []
downlist = []
for i in xlist:
uplist.append(i / 4)
for i in ylist:
downlist.append(i / 4)
for i in range(len(uplist)):
uplist[i] /= 2
for i in range(len(downlist)):
downlist[i] /= 2
func = lambda x, y: x if y in x else x + [y]
up = reduce(func, [[], ] + uplist)
func = lambda x, y: x if y in x else x + [y]
down = reduce(func, [[], ] + downlist)
return up,down
编写好排列方法,会用的
def bubble(l):
for index in range(len(l) - 1, 0 , -1):
for two_index in range(index):
if l[two_index] > l[two_index + 1]:
l[two_index], l[two_index + 1] = l[two_index + 1], l[two_index]
return l
最后的进行解析,拼装源码
def end():
orders = getUPDOWN()
orderx = orders[0]
ordery = orders[1]
print 'x:',orderx
print 'y:',ordery
orderX = [145, 157, 277, 265, 169, 181, 253, 241, 97, 109, 301, 289, 73, 85, 37, 25, 1, 13, 133, 121, 49, 61, 229,
217, 193, 205]
orderY = [157, 145, 265, 277, 181, 169, 241, 253, 109, 97, 289, 301, 85, 73, 25, 37, 13, 1, 121, 133, 61, 49, 217,
229, 205, 193]
print u'X:',orderX
print u'Y:',orderY
for x in range(26):
orderX[x] -= 1
orderX[x] /= 12
for y in range(26):
orderY[y] -= 1
orderY[y] /= 12
endX = []
endY = []
for i in range(len(orderX)):
for j in range(len(orderx)):
if orderx[j] == orderX[i]:
endX.append(i)
break
for i in range(len(orderY)):
for j in range(len(ordery)):
if ordery[j] == orderY[i]:
endY.append(i)
break
print 'endx:',endX
print 'endy:',endY
os.remove('/Users/zijiawang/Downloads/0_test.jpg')
os.remove('/Users/zijiawang/Downloads/1_test.jpg')
all = []
all = endX+endY
# print 'all:',all
#系统最多可实验5次
func = lambda x, y: x if y in x else x + [y]
all_old = reduce(func, [[], ] + all)
# print 'all_old:',all_old
all_end =bubble(all_old)
print 'all_end:',all_end
duandata = 1000
for i in range(1,len(all_end)):
if all_end[i]-1 in all_end:
pass
else:
duandata = all_end[i]
print u'断点为:',duandata
guess =[]
if duandata != 1000:
if endX != [] and endY!=[]:
print u'断点1'
guess = [duandata,all_end[0]]
elif endX ==[]:
print u'断点2'
guess = [duandata, endY[0]]
elif endY == []:
print u'断点3'
guess = [duandata, endX[0]]
else:
print u'无断点'
guess = [ all_end[0]]
end_guess =[]
print u'猜测的点:',guess
for i in guess:
end_guess.append(i * 12 - 38)
end_guess.append(i * 12 - 28)
end_guess.append(i * 12 - 20)
end_guess.append(i * 12 - 17)
end_guess.append(i * 12 - 15)
print u'猜测的位移量: ',end_guess
return end_guess
主函数
if __name__ == '__main__':
downloadImg()
color1 = getcolor(Image.open('/Users/zijiawang/Downloads/0_test.jpg').load())
color2 = getcolor(Image.open('/Users/zijiawang/Downloads/1_test.jpg').load())
guess = end()
for i in guess:
print i,wzj.title
try:
ele = wzj.find_element_by_xpath('//div[@class="gt_slider_knob gt_show"]')
except:
ele = wzj.find_element_by_xpath('//div[@class="gt_slider_knob gt_show moving"]')
ActionChains(wzj).click_and_hold(ele).perform()
ActionChains(wzj).move_to_element_with_offset(ele, 0,i).perform()
因每个网站的都大同小异,这里不写具体测试的网站了,以免对其造成压力。此算法属于高度定制的,应用其他网站需要改些参数,但是代码注释较小。需要注释,不明白的地方请留言哈。
来源:https://juejin.cn/post/7060143197963419679
标签:Python,selenium,验证码
0
投稿
猜你喜欢
golang实践-第三方包为私有库的配置方案
2023-06-25 06:05:32
python中查找excel某一列的重复数据 剔除之后打印
2022-10-27 06:34:00
网页绿色系配色应用实例
2008-08-26 11:51:00
python类的方法属性与方法属性的动态绑定代码详解
2023-07-02 03:31:26
mysql的case when字段为空,null的问题
2024-01-13 14:40:59
Linux下编译安装python3步骤
2021-11-07 17:50:04
vue 计时器组件的实现代码
2023-07-02 16:59:56
python实操案例练习(七)
2021-09-23 01:01:03
浅谈Keras的Sequential与PyTorch的Sequential的区别
2021-08-24 07:53:35
Python中模块string.py详解
2021-05-20 05:25:03
python使用xlrd模块读取xlsx文件中的ip方法
2022-12-26 13:42:00
pytorch 实现在一个优化器中设置多个网络参数的例子
2021-11-30 17:12:05
对比国内门户网站对Flash激活限制的处理
2007-08-23 11:29:00
SQL Server数据库查询优化的常用方法总结
2008-12-10 14:43:00
python中web框架的自定义创建
2023-09-18 14:54:24
对Go语言中的context包源码分析
2024-04-26 17:24:17
python文件夹分区的实现方法实例
2022-03-04 02:05:39
Python xlrd excel文件操作代码实例
2021-05-19 21:52:18
利用Python将多张图片合成视频的实现
2022-09-16 07:10:26
MySQL 数据库 like 语句通配符模糊查询小结
2024-01-20 03:41:30