0


selenium cv2 破解豆瓣验证码

哔哔哩哔哩的cv2破解知乎滑动验证视屏照着敲没敲出来。

后来发现这个人写的豆瓣的滑动验证破解,基本一样的。我就照着这个敲了。

利用selenium和cv2处理豆瓣滑块验证码_python selenium cv2.matchtemplate-CSDN博客

import re
import cv2
import requests
from selenium import  webdriver
from selenium.webdriver.common.by import  By
import  time
from selenium.webdriver.support.wait import  WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome()
driver.get("https://www.douban.com/")
# 等待加载10秒
WebDriverWait(driver,10,0.5)
# 找登录iframe
iframe0=driver.find_element(By.XPATH,'//*[@id="anony-reg-new"]/div/div[1]/iframe')
# print(iframe0)
driver.switch_to.frame(iframe0)
# 点切换账号密码登录
driver.find_element(By.XPATH,'/html/body/div[1]/div[1]/ul[1]/li[2]').click()
# input()
# 输入密码账号
driver.find_element(By.XPATH,'//*[@id="username"]').send_keys('15572848175')
driver.find_element(By.XPATH,'//*[@id="password"]').send_keys('123456')
driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
time.sleep(3)#等验证滑块出来

iframe1=driver.find_element(By.XPATH,'//*[@id="tcaptcha_iframe_dy"]')
driver.switch_to.frame(iframe1)

# 这个图片很麻烦 找验证码的位置
bg_style=driver.find_element(By.XPATH,'//*[@id="slideBg"]').get_attribute('style')
# print(xiao)
pattern=re.compile('background-image: url\("(.*?)"\);')
bg_url=re.findall(pattern,bg_style)[0]
print(bg_url)
resp=requests.get(url=bg_url)
with open('image/douban_slideBgWrap.jpg',mode='wb') as f:
    f.write(resp.content)
# cv2部分
# 定义一个处理图片缺口的函数,最后是返回x坐标,滑块移动不需要y坐标
def get_pos(image):
    # 首先使用高斯模糊去噪,噪声会影响边缘检测的准确性,因此首先要将噪声过滤掉
    blurred = cv2.GaussianBlur(image, (5, 5), 0, 0)

    # 边缘检测,得到图片轮廓
    canny = cv2.Canny(blurred, 200, 400)  # 200为最小阈值,400为最大阈值,可以修改阈值达到不同的效果

    # 轮廓检测
    # cv2.findContours()函数接受的参数为二值图,即黑白的(不是灰度图),所以读取的图像要先转成灰度的,再转成二值图,此处canny已经是二值图
    # contours:所有的轮廓像素坐标数组,hierarchy 轮廓之间的层次关系
    contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # print(contours, hierarchy)

    for i, contour in enumerate(contours):  # 对所有轮廓进行遍历
        M = cv2.moments(contour)  # 并计算每一个轮廓的力矩(Moment),就可以得出物体的质心位置
        # print(M)
        if M['m00'] == 0:
            cx = cy = 0
        else:
            # 得到质心位置,打印这个轮廓的面积和周长,用于过滤
            cx, cy = M['m10'] / M['m00'], M['m01'] / M['m00']
            print(cv2.contourArea(contour), cv2.arcLength(contour, True))

        # 判断这个轮廓是否在这个面积和周长的范围内
        if 5000 < cv2.contourArea(contour) < 8000 and 300 < cv2.arcLength(contour, True) < 500:
            print(cx)
            if cx < 300:
                continue
            print(cv2.contourArea(contour))
            print(cv2.arcLength(contour, True))

            # 外接矩形,x,y是矩阵左上点的坐标,w,h是矩阵的宽和高
            x, y, w, h = cv2.boundingRect(contour)

            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)  # 画出矩行
            # cv2.imshow('image', image)
            cv2.imwrite('111.jpg', image)  # 保存
            return x
    return 0

verify_img=cv2.imread('image/douban_slideBgWrap.jpg')#打开图片
x=get_pos(verify_img)#丢进去
print('移动距离',x)
# 开始拖动坐标
# 获取滑动条slider
slider=driver.find_element(By.XPATH,'//*[@id="tcOperation"]/div[6]')
result=int(x*0.41)-30 #这个那个作者写的x轴微调。

i=0
f_i=3#最多试三次
while i<f_i:

    try:
        # drag_and_drop_by_offset(source, xoffset, yoffset)y轴没什么用,就是0
        ActionChains(driver).drag_and_drop_by_offset(slider,result,0).perform()#动作链
        break
    except Exception as e:
        print('error=》》',e)
        i+=1
        print(f'重试第{i}次了')
time.sleep(3)#等3秒别那么快关闭
标签: python 爬虫 selenium

本文转载自: https://blog.csdn.net/weixin_43179869/article/details/135879935
版权归原作者 最强巫女 所有, 如有侵权,请联系我们删除。

“selenium cv2 破解豆瓣验证码”的评论:

还没有评论