爬虫学习笔记(十八) 您所在的位置:网站首页 验证码的测试点是什么 爬虫学习笔记(十八)

爬虫学习笔记(十八)

2024-07-11 02:13| 来源: 网络整理| 查看: 265

一、打码平台

点触验证码是一种常见的反爬手段。

解决方案有两种:一种是直接解决,这需要深度学习机器学习等图像处理技术,以大量的数据训练识别模型,最终达到模型足矣识别图片中的文字提示和应该点击的区域之间的对应关系。这需要非常专业的设备,比如TPU运算,和专业的开发和维护人员。 因此市面上有专业解决此类问题的OCR解决提供商。也就是第二种间接解决的方案,调用第三方接口。接下来将给大家介绍一款好用的第三方接口:超级鹰

二、超级鹰的使用

超级鹰验证码识别平台:https://www.chaojiying.com/

1、注册、登录超级鹰平台

2、点击“进入用户中心”,找到“软件ID”,选择“生成一个软件ID”,相当于做开发新建一个工程

3、下载开发样例 选择“开发文档”->“python”->“点击这里下载”

下载样例后解压结果:

超级鹰核心代码截图

创建类的参数:1、超级鹰登录账号; 2、超级鹰登录密码; 3、第2步创建的软件的ID PostPic方法参数:1、验证码图片字节 ;2、验证码的类型,不同的验证码,识别价格不一样 https://www.chaojiying.com/price.html

三、小案例:12306自动登录

(这里我们用selenium来进行自动登录)

1、先观察12306登录界面

我们需要用“账号登录”

from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://kyfw.12306.cn/otn/resources/login.html') driver.maximize_window() #窗口最大化 driver.implicitly_wait(5) #隐式等待 time.sleep(1) #强制等待,等待页面加载,作用同下 #1、点击“账号登录” account_login = driver.find_element_by_xpath('//li[@class="login-hd-account"]/a') account_login.click()

2、通过”网页调试工具“,用selenium进行找回密码自动输入

from login_message import LA,LP,CA,CP #LA:12306登录的账号 LP:12306登录密码 CA:超级鹰登录账号 CP:超级鹰登录密码 time.sleep(1) # 输入框输入登录账号 usename_put = driver.find_element_by_id('J-userName') usename_put.send_keys(LA) #LA:12306登录的账号 time.sleep(1) #输入框输入登录密码 passwd_put = driver.find_element_by_id('J-password') passwd_put.send_keys(LP) #LP:12306登录的密码

3、获取验证码图片

# 获取验证码图片 并保存在当前目录下 图片名:auth_code.png driver.find_element_by_id('J-loginImg').screenshot('auth_code.png')

4、通过超级鹰识别验证码并点击选择正确图标

from chaojiying import Chaojiying_Client #我把解压好的chaojiying.py放到项目目录下 from selenium.webdriver.common.action_chains import ActionChains #参数: 超级鹰登录账号 密码 软件ID chaojiying_Obj = Chaojiying_Client(username=CA,password=CP,soft_id=920548) #用二进制读的方式 打开保存的验证码图片 获得图片字节 with open('auth_code.png','rb') as f: im = f.read() #参数:字节图片 验证码的类型(参考: https://www.chaojiying.com/price.html) data = chaojiying_Obj.PostPic(im=im,codetype=9004) # print(type(data),data) 观察返回的数据类型及其结果 captcha = driver.find_element_by_id('J-loginImg') #通过id方式 定位验证码图片元素位置 postion_list = data['pic_str'].split('|') for postion in postion_list: #获取我们应该点击的图片位置 postion = postion.split(',') x = int(postion[0]) y = int(postion[1]) # 点击验证码图片中的正确位置 ActionChains(driver).move_to_element_with_offset(captcha,x,y).click().perform()

5、确认登录

# 确认登录 login_tag = driver.find_element_by_id('J-login') login_tag.click()

结果展示:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有