selenium + Chrome 解决滑动验证码四

声明:文章仅源自个人兴趣爱好,不涉及他用,侵权联系删。

逻辑和前三篇博客一样,这里就直接上代码了 。由于某某查网站迭代较快,这里是九月份的版本

# -*- coding: UTF-8 -*-
'''
@Author :Jason
@Date   :2019/9/10 18:19
@Desc   :Crate By Jason
某某查验证码 为注册时的验证码 没有账号
参考 https://blog.csdn.net/Asher117/article/details/90348758
    https://blog.csdn.net/genius_man/article/details/91377910
'''
import random

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from io import BytesIO
from time import sleep
from selenium.webdriver import ActionChains
from PIL import Image

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from urllib import request
import cv2
import numpy as np
from lxml.html import etree
import re, requests
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
# 设置无界面模式
from selenium.webdriver.chrome.options import Options

class Tyc_Login(object):
    def __init__(self):
        self.left = 55  # 定义一个左边的起点 缺口一般离图片左侧有一定的距离 有一个滑块
        self.url = 'https://www.tianyancha.com/login'
        self.chromedriverPath = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
        # chrome_options = Options() #以后就可以设置无界面
        # chrome_options.add_argument('--headless')
        # chrome_options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(executable_path=self.chromedriverPath)
        self.wait = WebDriverWait(self.driver, 20)  # 设置等待时间20秒
        self.username = "账号"
        self.password = "账号密码"

    def input_Info(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        sleep(1)
        # 点击密码登录
        self.driver.find_elements_by_xpath('//div[@class="title-tab text-center"]//div[2]')[0].click()

        sleep(2)
        self.inputuser = self.driver.find_element_by_css_selector('div.modulein:nth-child(2) > div:nth-child(2) > input:nth-child(1)')
        ''' 这个css选择器的解释:class中有modulein的第2个div 的 第2个儿子div 的 的第1个input标签 
        '''
        self.inputuser.send_keys(self.username)
        self.inputpwd = self.driver.find_element_by_css_selector('.input-pwd')
        self.inputpwd.send_keys(self.password)
        sleep(1)
        # 点击登录按钮,出现验证码图片
        login_button = self.driver.find_element_by_css_selector('div.modulein:nth-child(2) > div:nth-child(5)')
        login_button.click()
        sleep(3)


    def get_image(self):
        img = self.driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[1]/div[2]/div[1]')
        sleep(0.5)
        # 获取图片位子和宽高
        location = img.location
        size = img.size
        # 返回左上角和右下角的坐标来截取图片
        top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
            'width']
        # 截取第一张图片(无缺口的)
        screenshot = self.driver.get_screenshot_as_png()
        screenshot = Image.open(BytesIO(screenshot))
        captcha1 = screenshot.crop((left, top, right, bottom))
        print('--->', captcha1.size)
        captcha1.save('captcha1.png')
        # 截取第二张图片(有缺口的)
        self.driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]').click()
        sleep(4)
        img1 = self.driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[1]/div[2]/div[1]')
        sleep(0.5)
        location1 = img1.location
        size1 = img1.size
        top1, bottom1, left1, right1 = location1['y'], location1['y'] + size1['height'], location1['x'], location1['x'] + size1['width']
        screenshot = self.driver.get_screenshot_as_png()
        screenshot = Image.open(BytesIO(screenshot))
        captcha2 = screenshot.crop((left1, top1, right1, bottom1))
        captcha2.save('captcha2.png')
        print("两张图片保存完毕!!!")

    def is_similar(self, image1, image2, x, y):
        '''判断两张图片 各个位置的像素是否相同
        #image1:带缺口的图片
        :param image2: 不带缺口的图片
        :param x: 位置x
        :param y: 位置y
        :return: (x,y)位置的像素是否相同
        '''
        # 获取两张图片指定位置的像素点
        pixel1 = image1.load()[x, y]
        pixel2 = image2.load()[x, y]
        # 设置一个阈值 允许有误差
        threshold = 60
        # 彩色图 每个位置的像素点有三个通道
        if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
                pixel1[2] - pixel2[2]) < threshold:
            return True
        else:
            return False

    def get_diff_location(self):  # 获取缺口图起点
        captcha1 = Image.open('captcha1.png')
        captcha2 = Image.open('captcha2.png')
        for x in range(self.left, captcha1.size[0]):  # 从左到右 x方向
            for y in range(captcha1.size[1]):  # 从上到下 y方向
                if not self.is_similar(captcha1, captcha2, x, y):
                    return x  # 找到缺口的左侧边界 在x方向上的位置

    def get_move_track(self, gap):
        track = []  # 移动轨迹
        current = 0  # 当前位移
        # 减速阈值
        mid = gap * 2 / 5  # 前2/5段加速 后3/5段减速
        t = 0.2  # 计算间隔
        v = 0  # 初速度
        while current < gap:
            if current < mid:
                a = 3.5  # 加速度为+3.5
            else:
                a = -2  # 加速度为-2
            v0 = v  # 初速度v0
            v = v0 + a * t  # 当前速度
            move = v0 * t + 1 / 2 * a * t * t  # 移动距离
            current += move  # 当前位移
            track.append(round(move))  # 加入轨迹
        return track

    def move_slider(self, track):
        slider = self.driver.find_elements_by_xpath("/html/body/div[10]/div[2]/div[2]/div[2]/div[2]")[0]
        ActionChains(self.driver).click_and_hold(slider).perform()
        for x in track:  # 只有水平方向有运动 按轨迹移动
            ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()
        sleep(1)
        ActionChains(self.driver).release().perform()  # 松开鼠标


    def main(self):
        self.input_Info()
        self.get_image()
        gap = self.get_diff_location()
        print(gap,"<===缺口位置===>", gap-self.left)
        track = self.get_move_track(gap)
        track += [5, -5, 2, -2]  # 滑过去再滑过来,不然有可能被吃
        print("移动轨迹===>",track)
        self.move_slider(track)

        flag = False
        if '弗里茨·克莱斯勒' in str(self.driver.page_source):#随便找个判断条件,以用户名为例
            flag = True
        return flag

def main():
    T = Tyc_Login()
    try:
        for i in range(1, 5):
            flag = T.main()
            if flag == True:
                print("验证通过,拜拜")
                break
            sleep(10)
    except Exception as e:
        print("登录失败,", e)
    finally:
        T.driver.quit()

if __name__ == "__main__":
    main()

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Selenium 和 Webdriver 来解决滑动验证码的问题,你需要模拟一个真实的用户操作。下面是一些基本步骤: 1. 打开目标网站并输入用户名和密码; 2. 等待页面加载完成; 3. 找到滑动验证码的元素,获取其位置信息; 4. 找到滑块元素,并获取其位置信息; 5. 使用 `ActionChains` 类来模拟用户的拖动操作,将滑块拖动到正确的位置; 6. 检查验证码是否成功通过。 下面是一份示例代码,你需要根据实际情况进行调整: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time # 打开网站 driver = webdriver.Chrome() driver.get("http://www.example.com") # 输入用户名和密码 username_input = driver.find_element_by_id("username") password_input = driver.find_element_by_id("password") username_input.send_keys("your_username") password_input.send_keys("your_password") driver.find_element_by_id("submit").click() # 等待页面加载 time.sleep(5) # 找到验证码元素 slider = driver.find_element_by_id("slider") slider_bg = driver.find_element_by_id("slider_bg") # 获取元素位置信息 slider_location = slider.location slider_bg_location = slider_bg.location # 计算滑块需要移动的距离 distance = slider_bg_location['x'] + slider_bg.size['width'] - slider_location['x'] - slider.size['width'] # 模拟滑动操作 action = ActionChains(driver) action.click_and_hold(slider).perform() action.move_by_offset(distance, 0).perform() action.release().perform() # 检查验证码是否通过 if "验证成功" in driver.page_source: print("验证码通过") else: print("验证码未通过") # 关闭浏览器 driver.quit() ``` 需要注意的是,有些网站会对滑块进行一些加固,例如添加滑动速度限制、滑块位置随机等,这些都需要你自己去适配。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值