【每日一题】有关基础教程(第十五期)
优采云 发布时间: 2021-08-16 02:23【每日一题】有关基础教程(第十五期)
这篇文章给大家详细讲解Python完全识别验证码并自动登录的样例分析,小编觉得很实用,所以分享给大家作为参考。希望大家看完这篇文章后可以有所收获。
1、直接贴代码
<p>#!C:/Python27
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from pytesser import *
from PIL import Image,ImageEnhance,ImageFilter
from selenium.common.exceptions import NoSuchElementException,TimeoutException
import os,time
def before():
driver.get(src)
time.sleep(1)
driver.maximize_window() # 浏览器全屏显示
print ('\n浏览器全屏显示 ...')
def Convertimg():
imglocation = ("//*[@id='loginForm']/div[4]/div[2]/img[1]")
#下载验证码图片保存到本地
driver.save_screenshot('E:\\pythonScript\\Codeimages\\code.png')
#打开本地图片
im = Image.open('E:\\pythonScript\\Codeimages\\code.png')
left = driver.find_element_by_xpath(imglocation).location['x']
top = driver.find_element_by_xpath(imglocation).location['y']
right = driver.find_element_by_xpath(imglocation).location['x'] + driver.find_element_by_xpath(imglocation).size['width']
bottom = driver.find_element_by_xpath(imglocation).location['y'] + driver.find_element_by_xpath(imglocation).size['height']
im = im.crop((left, top, right, bottom))
im.save('E:\\pythonScript\\Codeimages\\screenshot.png')
print u"\n保存验证码图片完成"
#移除截屏的图片
os.remove('E:\\pythonScript\\Codeimages\\code.png')
print u"\n删除截屏图片完成"
#处理验证码图片
src = ('E:\\pythonScript\\Codeimages\\screenshot.png')
#调用裁剪图片方法
Cutedge(src)
#移除截屏的图片
os.remove('E:\\pythonScript\\Codeimages\\screenshot.png')
#灰化图片处理
im = Image.open('E:\\pythonScript\\Codeimages\\CutedgeImage.png')
imgry = im.convert('L')
#二值化处理
threshold = 100
table = []
for i in range(256):
if i