会用到的库的
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定位并识别图片验证码实现自动登录功能”评论...
更新日志
2026年05月04日
2026年05月04日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]


