原文转载自「刘悦的技术博客」https://v3u.cn/a_id_134

今天有同学提出了一个需求,老板让自动登录这个页面:https://www.dianxiaomi.com/index.htm,毫无疑问登录页面基本都会有在线验证码,要破解这个验证码当然不是啥难事,诚然可以自己去训练模型,然而大量的训练集测试数据还得自己去打码,说白了,不值当,有现成的接口可以用,何乐而不为,于是我们向万恶的百度伸出了魔爪。

首先申请百度api开发平台:https://cloud.baidu.com/

随后在人工智能-》文字识别 模块里建立一个应用

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图
image

建立成功后,会分配给你一个key和secret

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图1
image

只不过这个接口免费版的每秒最多只能请求两次,一天有500次的请求机会

这里推荐使用高精度的接口,普通的识别率太差了

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图2
image

第一步,获取access_token

res = requests.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的key&client_secret=你的秘钥")res = json.loads(str(res.text))token = res['access_token']

然后请求高精度识别接口,需要注意一点,这里图片需要转换成base64编码

temp_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + tokentemp_headers = {'Content-Type': 'application/x-www-form-urlencoded'}temp_file = open('./test.png', 'rb')temp_image = temp_file.read()temp_file.close()temp_data = {'image': base64.b64encode(temp_image)}temp_data = urllib.parse.urlencode(temp_data)temp_res = requests.post(url=temp_url, data=temp_data, headers=temp_headers)res = json.loads(str(temp_res.text))code = res['words_result'][0]['words']

百度接口会将识别出的验证码文字返回给你,当然了有一定误差是在所难免的

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图3
image

现在验证码识别已经搞定,问题的关键是,每次打开页面的时候,怎么将图片传给百度接口

其实很简单,我们可以考虑使用selenium自动化工具来截图,selenium是可以根据选择器做局部截图的

nodes = driver.find_element_by_id('loginImgVcode')nodes.screenshot('test.png')
Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图4
image

虽然可以截图,但是其实发现有东西干扰了验证码图片

原来是旁边讨厌的广告挡住了验证码,这样的干扰会让识别产生误差

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图5
image

其实这些html标签是可以移除的,使用selenium运行js代码来删除这些干扰元素

#删除元素js = "var elem = document.getElementsByClassName('dnf');elem[0].parentNode.removeChild(elem[0]);"driver.execute_script(js)js = "var elem = document.getElementsByClassName('qqshow');elem[0].parentNode.removeChild(elem[0]);"driver.execute_script(js)js = "var elem = document.getElementsByClassName('QR');elem[0].parentNode.removeChild(elem[0]);"driver.execute_script(js)

再截图一次,看到已经干净了很多

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图6
image

这里还有个技巧,可以提高百度api的识别准确度,就是利用opencv对验证码图片进行降噪去色的操作。

首先安装opencv

pip3 install opencv-python

然后对原图进行去色处理

import cv2    img = cv2.imread('./code.png', cv2.IMREAD_GRAYSCALE)  print("cv2.imread(imgfile, cv2.IMREAD_GRAYSCALE)结果如下:")  print('大小:{}'.format(img.shape))  print("类型:%s"%type(img))  print(img)    cv2.imwrite('code1.png', img)

去色降噪后的效果是这样的,可以提高识别成功率

Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面插图7
image

selenium驱动下载地址:http://npm.taobao.org/mirrors/chromedriver/

剩下的就很简单了,直接利用selenium输入账号密码以及验证码就可以自动登录,非常轻松,完整代码如下:

import requestsimport jsonimport base64import urllibfrom selenium import webdriverimport time#selenium 截图driver = webdriver.Chrome()driver.get('https://www.dianxiaomi.com/index.htm')time.sleep(1)#删除元素js = "var elem = document.getElementsByClassName('dnf');elem[0].parentNode.removeChild(elem[0]);"driver.execute_script(js)js = "var elem = document.getElementsByClassName('qqshow');elem[0].parentNode.removeChild(elem[0]);"driver.execute_script(js)js = "var elem = document.getElementsByClassName('QR');elem[0].parentNode.removeChild(elem[0]);"driver.execute_script(js)nodes = driver.find_element_by_id('loginImgVcode')nodes.screenshot('test.png')#用户名 exampleInputName exampleInputPassworddriver.find_element_by_id('exampleInputName').send_keys('18231327142')driver.find_element_by_id('exampleInputPassword').send_keys('密码')#请求百度api接口res = requests.get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的key&client_secret=你的秘钥")res = json.loads(str(res.text))token = res['access_token']temp_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + tokentemp_headers = {'Content-Type': 'application/x-www-form-urlencoded'}temp_file = open('./test.png', 'rb')temp_image = temp_file.read()temp_file.close()temp_data = {'image': base64.b64encode(temp_image)}temp_data = urllib.parse.urlencode(temp_data)temp_res = requests.post(url=temp_url, data=temp_data, headers=temp_headers)res = json.loads(str(temp_res.text))print(res)code = res['words_result'][0]['words']driver.find_element_by_id('loginVerifyCode').send_keys(code.strip().replace(' ',''))#登录按钮driver.find_element_by_id('loginBtn').click()time.sleep(10)driver.close()

原文转载自「刘悦的技术博客」 https://v3u.cn/a_id_134

文章转载于:https://www.jianshu.com/p/2c8d66d47013

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《Python3.7爬虫:实时api(百度ai)检测验证码模拟登录(Selenium)页面
   

还没有人抢沙发呢~