栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用python+selenium超级鹰破解图像识别验证码

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python+selenium超级鹰破解图像识别验证码

大家做爬虫的时候肯定会遇到很多验证码列如本文所指的图像识别:

 我在爬取拉勾网的时候过于频繁被跳转到了验证系统,真是令人头大

url=安全访问验证-拉勾网https://sec.lagou.com/verify.html?e=2&f=https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=

 

 我们需要先点击这里的验证按钮之后就会直接跳出图像验证码,在识别完成之后点击确认就会直接进去到职位页面

 

ok,我们先把前面代码写好
from  selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ChromeOptions   #这个包用来规避被检测的风险
import time  #延迟
from chaojiying import Chaojiying_Client
from selenium.webdriver import ActionChains  #动作链

#这两段代码规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
driver_path=r'C:Users哥斯拉AppDataLocalGoogleChromeApplicationchromedriver.exe' #定义好路径

driver=webdriver.Chrome(executable_path=driver_path,options=option) #初始化路径+规避检测

def register():
        driver.get('https://sec.lagou.com/verify.html?e=2&f=https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=')
那么这里我们就先找到元素位置在用selenium模拟点击,可以看到是在//a[@]' 这个标签下

verification = driver.find_element_by_xpath('//a[@]').click()
        time.sleep(2)
模拟点击之后去获取验证码所在的区域,然后在去截图此区域这里要写两步哈原因嘛后面在说

 这里还需要导入超级鹰,如果不了解的小伙伴可以看我写的另外一篇文章

(21条消息) 超级鹰的使用方式_m0_59874815的博客-CSDN博客https://blog.csdn.net/m0_59874815/article/details/121007373?spm=1001.2014.3001.5501

这里补充说明一点因为所有的电脑屏幕大小不同缩放也不一样,所以截图的时候要设置偏移量(不然截图出来不完整),我这里的话我因为懒得计算偏移量了所以我直接在系统里面设置了分辨率为百分百

 

 code_img=driver.find_element_by_xpath('//div[@]')  #获取验证码位置
        code_imgs=driver.find_element_by_xpath('//div[@]').screenshot_as_png  #截图验证码区域
        chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件id')  # 超级鹰账号
        result=chaojiying.PostPic(code_imgs,9008)['pic_str']
        kk = driver.find_element_by_xpath('//div[@]')  #点击确认按钮
通过超级鹰的识别可以看到返回的坐标是以 | 分割的那我们就给它提取出来并且放入一个列表

all_list=[]   #存储即将要点击点的坐标
        global x
        if '|' in result:
                list_1=result.split('|')
                count_1=len(list_1)
                for i in range(count_1):
                        xy_list=[]
                        x=int(list_1[i].split(',')[0])
                        y = int(list_1[i].split(',')[1])
                        xy_list.append(x)
                        xy_list.append(y)
                        all_list.append(xy_list)
        else:
                x = int(list_1[i].split(',')[0])
                y = int(list_1[i].split(',')[1])
                xy_list = []
                xy_list.append(x)
                xy_list.append(y)
                all_list.append(xy_list)
        #遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置点击操作
        print(all_list)
接下来我们需要遍历列表,使用动作链对每一个列表元素对应的x,y指定位置点击
 for l in all_list:
                x=l[0]
                y=l[1]
                ActionChains(driver).move_to_element_with_offset(code_img,x,y).click().perform()   #(这里面传入的是参照物也就是验证码的位置,x,y(xy是坐标))
                time.sleep(0.5)  #这里让他没隔0.5秒点击一次
        time.sleep(2)  
        kk.click()   #点击确认按钮
可以看到这里()我传入的是code_img这个,如果说开始的时候我们直接获取位置然后截图省略了一段代码这里在传入的话就会报错,所以截图和获取位置需要写两段分开来,后续测试也是能通过拉勾的验证系统就是吧识别率会低一些(这个没办法啦,图片太模糊了有时候的图片我自己都看不懂) 完整代码如下:
from  selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ChromeOptions   #这个包用来规避被检测的风险
import time  #延迟
from chaojiying import Chaojiying_Client
from selenium.webdriver import ActionChains  #动作链

#这两段代码规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
driver_path=r'C:Users哥斯拉AppDataLocalGoogleChromeApplicationchromedriver.exe' #定义好路径

driver=webdriver.Chrome(executable_path=driver_path,options=option) #初始化路径+规避检测

def register():
        driver.get('https://sec.lagou.com/verify.html?e=2&f=https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=')
        verification = driver.find_element_by_xpath('//a[@]').click()
        time.sleep(2)
        code_img=driver.find_element_by_xpath('//div[@]')  #获取验证码位置
        code_imgs=driver.find_element_by_xpath('//div[@]').screenshot_as_png  #截图验证码区域
        chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件ID')  # 超级鹰账号
        result=chaojiying.PostPic(code_imgs,9008)['pic_str']
        kk = driver.find_element_by_xpath('//div[@]')  #点击确认按钮
        print(result)
        all_list=[]   #存储即将要点击点的坐标
        global x
        if '|' in result:
                list_1=result.split('|')
                count_1=len(list_1)
                for i in range(count_1):
                        xy_list=[]
                        x=int(list_1[i].split(',')[0])
                        y = int(list_1[i].split(',')[1])
                        xy_list.append(x)
                        xy_list.append(y)
                        all_list.append(xy_list)
        else:
                x = int(list_1[i].split(',')[0])
                y = int(list_1[i].split(',')[1])
                xy_list = []
                xy_list.append(x)
                xy_list.append(y)
                all_list.append(xy_list)
        #遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置点击操作
        print(all_list)
        for l in all_list:
                x=l[0]
                y=l[1]
                ActionChains(driver).move_to_element_with_offset(code_img,x,y).click().perform()   #(这里面传入的是参照物也就是验证码的位置,x,y(xy是坐标))
                time.sleep(0.5) #每次点击间隔0.5秒
        time.sleep(2)
        kk.click()  #点击确认按钮



if __name__ == '__main__':
    register()

 

本文仅限于做技术交流学习,请勿用作任何非法用途!

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号