亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python識別驗證碼的思路及解決方案

瀏覽:13日期:2022-07-11 13:58:14

1、介紹

在爬蟲中經常會遇到驗證碼識別的問題,現在的驗證碼大多分計算驗證碼、滑塊驗證碼、識圖驗證碼、語音驗證碼等四種。本文就是識圖驗證碼,識別的是簡單的驗證碼,要想讓識別率更高,識別的更加準確就需要花很多的精力去訓練自己的字體庫。

識別驗證碼通常是這幾個步驟:

(1)灰度處理

(2)二值化

(3)去除邊框(如果有的話)

(4)降噪

(5)切割字符或者傾斜度矯正

(6)訓練字體庫

(7)識別

這6個步驟中前三個步驟是基本的,4或者5可根據實際情況選擇是否需要。

經常用的庫有pytesseract(識別庫)、OpenCV(高級圖像處理庫)、imagehash(圖片哈希值庫)、numpy(開源的、高性能的Python數值計算庫)、PIL的 Image,ImageDraw,ImageFile等。

2、實例

以某網站登錄的驗證碼識別為例:具體過程和上述的步驟稍有不同。

python識別驗證碼的思路及解決方案

首先分析一下,驗證碼是由4個從0到9等10個數字組成的,那么從0到9這個10個數字沒有數字只有第一、第二、第三和第四等4個位置。那么計算下來共有40個數字位置,如下:

python識別驗證碼的思路及解決方案

那么接下來就要對驗證碼圖片進行降噪、分隔得到上面的圖片。以這40個圖片集作為基礎。

對要驗證的驗證碼圖片進行降噪、分隔后獲取四個類似上面的數字圖片、通過和上面的比對就可以知道該驗證碼是什么了。

以上面驗證碼2837為例:

1、圖片降噪

python識別驗證碼的思路及解決方案

2、圖片分隔

python識別驗證碼的思路及解決方案

3、圖片比對

通過比驗證碼降噪、分隔后的四個數字圖片,和上面的40個數字圖片進行哈希值比對,設置一個誤差,max_dif:允許最大hash差值,越小越精確,最小為0。

python識別驗證碼的思路及解決方案

這樣四個數字圖片通過比較后獲取對應是數字,連起來,就是要獲取的驗證碼。

完整代碼如下:

#coding=utf-8import osimport refrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport timefrom selenium.webdriver.common.action_chains import ActionChainsimport collectionsimport mongoDbBaseimport numpyimport imagehashfrom PIL import Image,ImageFileimport datetimeclass finalNews_IE: def __init__(self,strdate,logonUrl,firstUrl,keyword_list,exportPath,codepath,codedir):self.iniDriver()self.db = mongoDbBase.mongoDbBase()self.date = strdateself.firstUrl = firstUrlself.logonUrl = logonUrlself.keyword_list = keyword_listself.exportPath = exportPathself.codedir = codedirself.hash_code_dict ={}for f in range(0,10): for l in range(1,5):file = os.path.join(codedir, 'codeLibrarycode' + str(f) + ’_’+str(l) + '.png')# print(file)hash = self.get_ImageHash(file)self.hash_code_dict[hash]= str(f) def iniDriver(self):# 通過配置文件獲取IEDriverServer.exe路徑IEDriverServer = 'C:Program FilesInternet ExplorerIEDriverServer.exe'os.environ['webdriver.ie.driver'] = IEDriverServerself.driver = webdriver.Ie(IEDriverServer) def WriteData(self, message, fileName):fileName = os.path.join(os.getcwd(), self.exportPath + ’/’ + fileName)with open(fileName, ’a’) as f: f.write(message) # 獲取圖片文件的hash值 def get_ImageHash(self,imagefile):hash = Noneif os.path.exists(imagefile): with open(imagefile, ’rb’) as fp:hash = imagehash.average_hash(Image.open(fp))return hash # 點降噪 def clearNoise(self, imageFile, x=0, y=0):if os.path.exists(imageFile): image = Image.open(imageFile) image = image.convert(’L’) image = numpy.asarray(image) image = (image > 135) * 255 image = Image.fromarray(image).convert(’RGB’) # save_name = 'D:workpython36_crawlVeriycodemode_5590.png' # image.save(save_name) image.save(imageFile) return image #切割驗證碼 # rownum:切割行數;colnum:切割列數;dstpath:圖片文件路徑;img_name:要切割的圖片文件 def splitimage(self, imagePath,imageFile,rownum=1, colnum=4):img = Image.open(imageFile)w, h = img.sizeif rownum <= h and colnum <= w: print(’Original image info: %sx%s, %s, %s’ % (w, h, img.format, img.mode)) print(’開始處理圖片切割, 請稍候...’) s = os.path.split(imageFile) if imagePath == ’’:dstpath = s[0] fn = s[1].split(’.’) basename = fn[0] ext = fn[-1] num = 1 rowheight = h // rownum colwidth = w // colnum file_list =[] for r in range(rownum):index = 0for c in range(colnum): # (left, upper, right, lower) # box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight) if index < 1:colwid = colwidth + 6 elif index < 2:colwid = colwidth + 1 elif index < 3:colwid = colwidth box = (c * colwid, r * rowheight, (c + 1) * colwid, (r + 1) * rowheight) newfile = os.path.join(imagePath, basename + ’_’ + str(num) + ’.’ + ext) file_list.append(newfile) img.crop(box).save(newfile, ext) num = num + 1 index += 1 return file_list def compare_image_with_hash(self, image_hash1,image_hash2, max_dif=0):'''max_dif: 允許最大hash差值, 越小越精確,最小為0推薦使用'''dif = image_hash1 - image_hash2# print(dif)if dif < 0: dif = -difif dif <= max_dif: return Trueelse: return False # 截取驗證碼圖片 def savePicture(self):self.driver.get(self.logonUrl)self.driver.maximize_window()time.sleep(1)self.driver.save_screenshot(self.codedir +'Temp.png')checkcode = self.driver.find_element_by_id('checkcode')location = checkcode.location # 獲取驗證碼x,y軸坐標size = checkcode.size # 獲取驗證碼的長寬rangle = (int(location[’x’]), int(location[’y’]), int(location[’x’] + size[’width’]), int(location[’y’] + size[’height’])) # 寫成我們需要截取的位置坐標i = Image.open(self.codedir +'Temp.png') # 打開截圖result = i.crop(rangle) # 使用Image的crop函數,從截圖中再次截取我們需要的區域filename = datetime.datetime.now().strftime('%M%S')filename =self.codedir +'Temp_code.png'result.save(filename)self.clearNoise(filename)file_list = self.splitimage(self.codedir,filename)verycode =’’for f in file_list: imageHash = self.get_ImageHash(f) for h,code in self.hash_code_dict.items():flag = self.compare_image_with_hash(imageHash,h,0)if flag: # print(code) verycode+=code breakprint(verycode)self.driver.close() def longon(self):self.driver.get(self.logonUrl)self.driver.maximize_window()time.sleep(1)self.savePicture()accname = self.driver.find_element_by_id('username')# accname = self.driver.find_element_by_id('//input[@id=’username’]')accname.send_keys(’ctrchina’)accpwd = self.driver.find_element_by_id('password')# accpwd.send_keys(’123456’)code = self.getVerycode()checkcode = self.driver.find_element_by_name('checkcode')checkcode.send_keys(code)submit = self.driver.find_element_by_name('button')submit.click()

實例補充:

# -*- coding: utf-8 -*import sysreload(sys)sys.setdefaultencoding( 'utf-8' )import reimport requestsimport ioimport osimport jsonfrom PIL import Imagefrom PIL import ImageEnhancefrom bs4 import BeautifulSoupimport mdataclass Student: def __init__(self, user,password): self.user = str(user) self.password = str(password) self.s = requests.Session() def login(self): url = 'http://202.118.31.197/ACTIONLOGON.APPPROCESS?mode=4' res = self.s.get(url).text imageUrl = ’http://202.118.31.197/’+re.findall(’<img src='http://www.aoyou183.cn/bcjs/(.+?)' width='55'’,res)[0] im = Image.open(io.BytesIO(self.s.get(imageUrl).content)) enhancer = ImageEnhance.Contrast(im) im = enhancer.enhance(7) x,y = im.size for i in range(y): for j in range(x): if (im.getpixel((j,i))!=(0,0,0)): im.putpixel((j,i),(255,255,255)) num = [6,19,32,45] verifyCode = '' for i in range(4): a = im.crop((num[i],0,num[i]+13,20)) l=[] x,y = a.size for i in range(y): for j in range(x): if (a.getpixel((j,i))==(0,0,0)): l.append(1) else: l.append(0) his=0 chrr=''; for i in mdata.data: r=0; for j in range(260): if(l[j]==mdata.data[i][j]): r+=1 if(r>his): his=r chrr=i verifyCode+=chrr # print '輔助輸入驗證碼完畢:',verifyCode data= { ’WebUserNO’:str(self.user), ’Password’:str(self.password), ’Agnomen’:verifyCode, } url = 'http://202.118.31.197/ACTIONLOGON.APPPROCESS?mode=4' t = self.s.post(url,data=data).text if re.findall('images/Logout2',t)==[]: l = ’[0,'’+re.findall(’alert((.+?));’,t)[1][1][2:-2]+’']’+' '+self.user+' '+self.password+'n' # print l # return ’[0,'’+re.findall(’alert((.+?));’,t)[1][1][2:-2]+’']’ return [False,l] else: l = ’登錄成功 ’+re.findall(’!&nbsp;(.+?)&nbsp;’,t)[0]+' '+self.user+' '+self.password+'n' # print l return [True,l] def getInfo(self): imageUrl = ’http://202.118.31.197/ACTIONDSPUSERPHOTO.APPPROCESS’ data = self.s.get(’http://202.118.31.197/ACTIONQUERYBASESTUDENTINFO.APPPROCESS?mode=3’).text #學籍信息 data = BeautifulSoup(data,'lxml') q = data.find_all('table',attrs={’align’:'left'}) a = [] for i in q[0]: if type(i)==type(q[0]) : for j in i : if type(j) ==type(i): a.append(j.text) for i in q[1]: if type(i)==type(q[1]) : for j in i : if type(j) ==type(i): a.append(j.text) data = {} for i in range(1,len(a),2): data[a[i-1]]=a[i] # data[’照片’] = io.BytesIO(self.s.get(imageUrl).content) return json.dumps(data) def getPic(self): imageUrl = ’http://202.118.31.197/ACTIONDSPUSERPHOTO.APPPROCESS’ pic = Image.open(io.BytesIO(self.s.get(imageUrl).content)) return pic def getScore(self): score = self.s.get(’http://202.118.31.197/ACTIONQUERYSTUDENTSCORE.APPPROCESS’).text #成績單 score = BeautifulSoup(score, 'lxml') q = score.find_all(attrs={’height’:'36'})[0] point = q.text print point[point.find(’平均學分績點’):] table = score.html.body.table people = table.find_all(attrs={’height’ : ’36’})[0].string r = table.find_all(’table’,attrs={’align’ : ’left’})[0].find_all(’tr’) subject = [] lesson = [] for i in r[0]: if type(r[0])==type(i): subject.append(i.string) for i in r: k=0 temp = {} for j in i: if type(r[0])==type(j): temp[subject[k]] = j.string k+=1 lesson.append(temp) lesson.pop() lesson.pop(0) return json.dumps(lesson) def logoff(self): return self.s.get(’http://202.118.31.197/ACTIONLOGOUT.APPPROCESS’).textif __name__ == '__main__': a = Student(20150000,20150000) r = a.login() print r[1] if r[0]: r = json.loads(a.getScore()) for i in r: for j in i: print i[j], print q = json.loads(a.getInfo()) for i in q: print i,q[i] a.getPic().show() a.logoff()

到此這篇關于python識別驗證碼的思路及解決方案的文章就介紹到這了,更多相關python識別驗證碼的思路是什么內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 精品久久视频 | 免费变态视频网站 | 亚洲综合自拍 | jizzjizz日本护士 | 久久精品网站免费观看 | 毛片啪啪啪 | 黄色网zhan| 亚洲国产精品热久久2022 | 亚洲1区2区3区4区 | 免费超爽成年大片黄 | 欧美日韩亚毛片免费观看 | 91在线 在线播放 | 亚洲欧美制服丝袜一区二区三区 | 国产在线精品观看一区 | 图片专区亚洲色图 | 国产手机精品自拍视频 | 麻豆视频在线观看 | 国产成人永久免费视 | 99精品国产一区二区三区 | 国产精品久久久久这里只有精品 | 国产精品一区二区不卡 | 成 人 免费 黄 色 视频 | 免费中日高清无专码有限公司 | 尤物视频在线免费 | 成年人xxx| 黑人性视频做爰全过程视频 | 欧美精品国产综合久久 | 日韩在线一区二区三区视频 | 免费人成网站在线高清 | 美国黄色片免费看 | 深夜做爰性大片中文 | 国产大片91精品免费观看男同 | 国产欧美日韩专区 | 免费特黄级夫费生活片 | 亚洲成人三级 | 美女免费精品高清毛片在线视 | 黑人巨大解禁久久中文字幕 | 欧美一级片在线视频 | 激情另类国内一区二区视频 | 国产一级黄色影片 | 亚洲在线播放视频 |