您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Python如何實(shí)現(xiàn)正方系統(tǒng)滑動(dòng)驗(yàn)證碼識(shí)別”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Python如何實(shí)現(xiàn)正方系統(tǒng)滑動(dòng)驗(yàn)證碼識(shí)別”這篇文章吧。
點(diǎn)擊滑動(dòng)按鈕,將發(fā)送一個(gè)請(qǐng)求到 /zfcaptchaLogin
請(qǐng)求內(nèi)容
"type": "verify" "rtk": "6cfab177-afb2-434e-bacf-06840c12e7af" "time": "1624611806948" "mt": "W3sieCI6OTY1LCJ5IjoxNjksInQiOjE2MjQ2MTE4MDY4Njh9LHsieCI6OTY1LCJ5IjoxNjksInQiOjE2MjQ2MTE4MDY5NDh9XQ==" "instanceId": "zfcaptchaLogin" "extend": "eyJhcHBOYW1lIjoiTmV0c2NhcGUiLCJ1c2VyQWdlbnQiOiJNb3ppbGxhLzUuMCAoTWFjaW50b3NoOyBJbnRlbCBNYWMgT1MgWCAxMF8xNV83KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvOTEuMC40NDcyLjEwNiBTYWZhcmkvNTM3LjM2IiwiYXBwVmVyc2lvbiI6IjUuMCAoTWFjaW50b3NoOyBJbnRlbCBNYWMgT1MgWCAxMF8xNV83KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvOTEuMC40NDcyLjEwNiBTYWZhcmkvNTM3LjM2In0="
通過 base64 解密 mt和 extend 得出解密的數(shù)值
# mt [{"x":965,"y":169,"t":1624611806868},{"x":965,"y":169,"t":1624611806948}] # extend {"appName":"Netscape","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36","appVersion":"5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36"}
mt 為用戶的點(diǎn)擊行為,x為X軸上的值,y為Y軸上的值,t為時(shí)間戳。通過大量點(diǎn)擊分析,發(fā)現(xiàn)x值最小值為 950,得出950 為 X軸的起點(diǎn),y值隨機(jī)無固定值。
extend 為請(qǐng)求頭部?jī)?nèi)容
將圖像灰度化,通過getpixel可以獲取圖像某一點(diǎn)的顏色值, 顏色值越高代表圖像越淺,所以尋找縱向連續(xù)50個(gè)像素點(diǎn)均是 getpixel(x+1, y) > getpixel(x, y)(X軸=x 比 X軸=x+1 顏色淺)
并掃描圖像,當(dāng)x=130、掃描高度=50時(shí),的顏色比x+1時(shí)深。
from PIL import Image import matplotlib.pyplot as plt import numpy as np scanf_height= 50 # 掃描的高度 img = Image.open("zfcaptchaLogin.png") def contrast(imgl, x, y,scanf_height): # 黃框顏色值比紅框顏色值淺的個(gè)數(shù) count = 0 for i in range(scanf_height): if imgl.getpixel((x+1, y+i)) > imgl.getpixel((x, y+i)): count += 1 # 當(dāng) count = scanf_height, 代表黃條區(qū)域 整體 紅條區(qū)域 顏色值淺,則是驗(yàn)證碼框位置 return count def scanf(img): imgx, imgy = img.size imgl = img.convert('L') # 圖像灰度化 plt.yticks([]) plt.xticks([i for i in range(0, imgx, 25)]) plt.imshow(img) plt.pause(0.5) for y in range(0, imgy-scanf_height, 10): plt.pause(0.01) plt.clf() plt.yticks([]) plt.xticks([i for i in range(0, imgx, 25)]) plt.imshow(imgl, cmap=plt.cm.gray) for x in range(1, imgx-1, 1): plt.pause(0.0001) plt.plot([x-1,x-1], [y, y+scanf_height], color='white') plt.plot([x,x], [y, y+scanf_height], color='red') plt.plot([x+1,x+1], [y, y+scanf_height], color='yellow') count = contrast(imgl, x,y, scanf_height) plt.title('count: {}'.format(count) ) print("x,y=[{}, {}], 黃條區(qū)域值比紅條區(qū)域顏色值淺的個(gè)數(shù):{}".format(x,y, count)) if count == scanf_height: return scanf(img) plt.show()
優(yōu)化代碼計(jì)算x,y值
import json import random import time from io import BytesIO from PIL import Image class ZfCaptchaRecognit(object): def __init__(self, img_path): self.img = Image.open(img_path) def _get_xy(self): # 計(jì)算 x,y 值 def _is_dividing_line(img_l, x, y): for n in range(50): # 尋找縱向連續(xù)50個(gè)像素點(diǎn)均是 X=x 比 X=x+1 顏色深 if y + n >= img_l.size[1] or x >= img_l.size[0] - 1: return False if img_l.getpixel((x + 1, y + n)) - img_l.getpixel((x, y + n)) < 2: return False return True img_l = self.img.convert("L") for x in range(img_l.size[0]): for y in range(img_l.size[1]): if _is_dividing_line(img_l, x, y): return (x, y) def show_tag(self): # 展示 切分點(diǎn) X, Y = self._get_xy() img2 = Image.new("RGB", self.img.size, (255, 255, 255)) for x in range(self.img.size[0]): for y in range(self.img.size[1]): pix = self.img.getpixel((x, y)) img2.putpixel((x, y), pix) if x == X or y == Y: img2.putpixel((x, y), 225) img2.save("show_tag.png") img2.show() captcha = ZfCaptchaRecognit("zfcaptchaLogin.png") captcha.show_tag()
通過 步驟一得出x值最小為950,y值無規(guī)律
則提交參數(shù)mt的大致格式數(shù)據(jù)是
[{ "x":950+ 滑動(dòng)距離 + 浮動(dòng)值, # 浮動(dòng)值的范圍通過分析提交參數(shù)得出在10~20內(nèi) "y":random.randint(150, 190), # 無規(guī)律,暫定150到190范圍內(nèi) "t":int(time.time() * 1000)}, # 時(shí)間戳 ...]
獲取mt 參數(shù)
import json import random import time from io import BytesIO from PIL import Image class ZfCaptchaRecognit(object): def __init__(self, img_stream): obj = BytesIO(img_stream) self.img = Image.open(obj) def _get_xy(self): ... def generate_payload(self): base_x = 950 X, Y = self._get_xy() payloads = [{"x": base_x + random.randint(5, 20), "y": random.randint(150, 190), "t": int(time.time() * 1000)}] for i in range(random.randint(15, 30)): # 在上一個(gè)參數(shù)基礎(chǔ)下浮動(dòng) last_payload = payloads[-1].copy() payloads[0]["x"] += random.choice([0] * 8 + [1, -1] * 2 + [2, -2]) last_payload["t"] += random.randint(1, 20) last_payload["y"] += random.choice([0] * 8 + [1, -1] * 2 + [2, -2]) payloads.append(last_payload) payloads[-1]["x"] = base_x + random.randint(10, 20) + X return json.dumps(payloads) captcha = ZfCaptchaRecognit("zfcaptchaLogin.png") captcha. generate_payload()
以上是“Python如何實(shí)現(xiàn)正方系統(tǒng)滑動(dòng)驗(yàn)證碼識(shí)別”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。