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()
发布了128 篇原创文章 · 获赞 95 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/qq_36853469/article/details/102857359