首页 > 其他 > 详细

爬虫7 验证码解析

时间:2020-05-12 18:14:07      阅读:62      评论:0      收藏:0      [点我收藏+]

简述:

  登录某些网站,需要用户填写验证码等验证方式,暂时提供两种爬虫解析验证码的方法

    1. 下载验证码图片,手动填写验证码

    2.

      a. 图像识别,使用pytesseract库识别图像中的字符

      b. 云打码,使用网上其它人的图像识别工具,获取到验证码

     注: 用户登录时,要用session来记录登录信息,保持会话。

      1. session = requests.Session()

      2. r = session.get(url, headers=...)

图像识别实例:

  

from PIL import Image
from pytesseract import image_to_string

# 一. 处理简单的,干扰少或无干扰的验证码图像
img = Image.open(timg2.jpg)
print(--------, img)
code = image_to_string(img)
print(code)

# 二、处理有复杂干扰的验证码图像,需根据实际情况修改代码
""" 1. 处理图像,降噪,去除干扰,转化灰度等等,需要根据真实情况来设计 2. 字符识别 """ def clear_image(image): image = image.convert(RGB) width = image.size[0] height = image.size[1] noise_color = get_noise_color(image) for x in range(width): for y in range(height): # 清除边框和干扰色 rgb = image.getpixel((x, y)) if (x == 0 or y == 0 or x == width - 1 or y == height - 1 or rgb == noise_color or rgb[1] > 100): image.putpixel((x, y), (255, 255, 255)) return image def get_noise_color(image): for y in range(1, image.size[1] - 1): # 获取第2列非白的颜色 (r, g, b) = image.getpixel((2, y)) if r < 255 and g < 255 and b < 255: return (r, g, b) image = Image.open(code4.png) image = clear_image(image) # 转化为灰度图 imgry = image.convert(L) code = image_to_string(imgry) imgry.save("imgry1.png") with open("code.txt", "w") as f: print(code) f.write(str(code))

 

爬虫7 验证码解析

原文:https://www.cnblogs.com/leafchen/p/12877502.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!