您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python+selenium怎么破解拼圖驗證碼”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Python+selenium怎么破解拼圖驗證碼”文章能幫助大家解決問題。
很多網站都有拼圖驗證碼
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()
關于“Python+selenium怎么破解拼圖驗證碼”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。