12306的点击选中验证码
# @Time : 2020/7/24 10:57 # @Author : ZhangYangyang # @Software: PyCharm import requests # img图片获取的链接 img_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand' session = requests.session() req = session.get(img_url).content with open('code.png', 'wb') as f: f.write(req) # 存储图片 codeImg = input('请输入验证码:') check_url = 'https://kyfw.12306.cn/passport/captcha/captcha-check' data = { 'answer': codeImg, 'rand': 'sjrand', 'login_site': 'E', } cons = session.get(check_url, params=data).text print(cons)
分析:
分析网页发现获取图片的链接是:https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand
我们把图片存储起来,根据像素确定你要点击的东西
图片验证码
''' 验证码内可能有多余线条干扰图片识别 可进行转灰度,二值化等操作 先将原图转为灰度图像,再指定二值化阈值 ''' import tesserocr from PIL import Image image = Image.open('code.jpg') #传入L,将图片转换为灰度图像,去掉多余线条 image = image.convert('L') threshold = 127 table = [] for i in range(256): if i<threshold: table.append(0) else: table.append(1) image = image.point(table,'1') result = tesserocr.image_to_text(image) print(result)
分析:
我们需要安装tesserocr以及他依赖的环境,配置环境变量,这里不做细说
当然了,这种识别图片上验证码数字字母正确率不保证,如果自己测试可以用自己输入验证码的方式登录