会用到的库的
1、selenium的webdriver
2、tesserocr或者pytesseract进行图像识别
3、pillow的Image进行图片处理
from selenium import webdriver import tesserocr from PIL import Image
tesserocr的安装.
获取验证码图片方法1:
def get_code_image(file_name): driver.save_screenshot(file_name) # 截取整个屏幕并保存 code_element = driver.find_element_by_class_name("verify_code_img___1Mei_") # 定位到验证码元素 left = code_element.location['x'] # 定位到截图位置 top = code_element.location['y'] right = code_element.size['width'] + left bottom = code_element.size['height'] + top im = Image.open(file_name) # 从文件读取截图,截取验证码位置再次保存 img = im.crop((left, top, right, bottom)) img.save(file_name) return file_name
获取验证码图片方法2:
def get_code_image(file_name): code_element = driver.find_element_by_class_name("verify_code_img___1Mei_") # 定位到验证码元素 code_element.screenshot(file_name)
注:此方法截图时屏幕会闪动,可能引发bug,如下图,目前没有解决
处理验证码图片
def deal_code_image(file_name): image = Image.open(file_name) # image.show() #查看处理前的图片 # 处理图片去除干扰 # 将图片转化为灰度图像 image = image.convert('L') threshold = 90 # 设置临界值,临界值可调试 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) image = image.point(table, '1') # image.show() #查看处理后的图片 # 1:使用tesseract库识别图片中的验证码 # res = tesserocr.image_to_text(image) # 2:使用pytesseract库识别图片中的验证码 res = pytesseract.image_to_string(image) # print(res) #查看识别出来的文案 res = res.replace(" ", "") #去除结果中的空格 return res
处理前的图片,有干扰,无法识别
处理后的图片,基本可以识别
识别结果不一定准确,如果验证码输入错误,可以点击换一张图片再次识别,多次尝试,本次不做说明
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“python3定位并识别图片验证码实现自动登录功能”评论...
更新日志
2024年07月03日
2024年07月03日
- 原画壁纸及美图第255期,无水印可自取
- 群星.1991-宝丽金冬日浓情【宝丽金】【WAV+CUE】
- 胡杨林.2006-香水有毒【太格印象】【WAV+CUE】
- 陈晓东.2002-从未忘记2CD【环球】【WAV+CUE】
- 绝区零珂蕾妲阵容怎么搭配好 珂蕾妲队伍搭配攻略
- 绝区零暴击鸣徽有哪些 绝区零暴击鸣徽效果介绍
- 魔兽世界奥格瑞玛法师训练师在哪 奥格瑞玛法师训练师位置介绍
- 《艾尔登法环》dlc带电祷告位置介绍
- 《幻兽帕鲁》泰锋boss坐标位置介绍
- 《山河旅探》6月份更新内容介绍
- 群星.2012《高雄国际Hi-End音响大展纪念发烧金碟(最响宣言)》[WAV分轨].
- 黄宣2021-Beanstalk[否极泰来][WAV+CUE]
- 康康1999-催泪[阿尔法][WAV+CUE]
- 人总有崩溃的时候:你玩过哪些能治愈人心的游戏?
- SteamDeck新增最热玩榜单:综合衡量玩家数和时长