day5 - 超级鹰验证码和B站滑动验证

  • 导入超级鹰的包在项目下面

1. e21网站验证码识别

"""__author__= 雍新有"""
from io import BytesIO

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from PIL import Image

from chaojiying_Python.chaojiying import main1

browser = webdriver.Chrome()
browser.get('http://bm.e21cn.com/login/user')
wait = WebDriverWait(browser, 10)
# 将屏幕的宽高自定义,或者执行js实现拖拽(window.scrollTo(1000, 1000))
# browser.set_window_size(1500, 1300)


def screen_big_png():
    # 获取整个窗口的图片
    big_screen = browser.get_screenshot_as_png()
    # 保存  BytesIO -- 读取二进制文件
    img = Image.open(BytesIO(big_screen))
    print(img)
    img.save('a1.png')
    return img


def get_position():
    # 显示等待
    img = wait.until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="imgCheckCode"]'))
    )
    print(img.location)
    print(img.size)
    size = img.size
    location = img.location
    # 左上角定位
    x1 = location['x'] * 1.25
    y1 = location['y'] * 1.25
    # 右下角定位
    x2 = x1 + size['width']*1.28
    y2 = y1 + size['height']*1.28
    return (x1, y1, x2, y2)


def screen_small_png(big_png):
    # 先获取验证码的位置,x和y
    x1, y1, x2, y2 = get_position()
    img = big_png.crop((x1, y1, x2, y2))
    img.save('a2.png')


if __name__ == '__main__':
    # 扣大图
    big_png = screen_big_png()
    # 扣小图
    screen_small_png(big_png)
    # 超级鹰校验
    result = main1('a2.png')
    code = result['pic_str']
    print(code)
    # 模拟登陆
    # 显示等待,获取

2. B站极验验证码

"""__author__= 雍新有"""
import time
from io import BytesIO

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image


class BiliSpider():

    def __init__(self):
        self.browser = webdriver.Chrome()
        self.wait = WebDriverWait(self.browser, 30)
        self.url = 'https://passport.bilibili.com/login'
        self.username = 'coco'
        self.password = '123456'
        self.filename1 = 'big1.png'
        self.filename2 = 'big2.png'
        self.smallname1 = 's1.png'
        self.smallname2 = 's2.png'

    # def __del__(self):
    #     # 类执行完后会自动调用这个函数
    #     self.browser.close()

    def login_open(self):
        # 打开B站登陆页面,并输入账号密码,最后点击登陆按钮
        self.browser.get(self.url)
        # 账号输入框
        name_input = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="login-username"]'))
        )
        name_input.clear()
        name_input.send_keys(self.username)
        # 密码输入框
        password_input = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="login-passwd"]'))
        )
        password_input.clear()
        password_input.send_keys(self.password)
        # 点击登陆按钮
        button = self.wait.until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="geetest-wrap"]/ul/li[5]/a[1]'))
        )
        button.click()
        # 目的是让验证码加载出来,手动的睡眠几秒
        time.sleep(3)

    def save_big_png(self, filename, smallname):
        # 实现截大图
        img = self.browser.get_screenshot_as_png()
        img = Image.open(BytesIO(img))
        img.save(filename)
        # 截取小图
        small_png = self.crop_png(img, smallname)
        return small_png

    def screen_png(self):
        # 截大图
        # 横向滚动
        # js = 'window.scrollTo(1000, 0)'
        # self.browser.execute_script(js)
        # 截取,保存有缺口的大图, 返回小图
        img_s1 = self.save_big_png(self.filename1, self.smallname1)
        # 隐藏验证码中的缺口,然后在截取
        js = 'document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display="block"'
        self.browser.execute_script(js)
        img_s2 = self.save_big_png(self.filename2, self.smallname2)
        return img_s1, img_s2

    def get_position(self):
        # 获取左上角和右下角的横纵坐标位置
        chapter = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]'))
        )
        location = chapter.location
        size = chapter.size
        x1 = location['x']
        y1 = location['y']
        x2 = x1 + size['width']
        y2 = y1 + size['height']
        return x1, y1, x2, y2

    def crop_png(self, img, filename):
        # 截取小图,有缺口小图和无缺口小图
        x1, y1, x2, y2 = self.get_position()
        small_img = img.crop((x1, y1, x2, y2))
        small_img.save(filename)
        return small_img

    def compare_img(self, img1, img2, x, y):
        # 比较图片像素点,像素点相似返回True,否则False
        # getpixel((x, y)) , img1.load()[x, y] - 获取图片像素点的rgba值

        pix1 = img1.load()[x, y]
        pix2 = img2.load()[x, y]
        # 阈值 - 像素偏差
        a = 60
        if abs(pix1[0] - pix2[0]) < a and \
            abs(pix1[1] - pix2[1]) < a and \
            abs(pix1[2] - pix2[2]) < a and \
            abs(pix1[3] - pix2[3]) < a:
            # 两个像素点相差不大
            return True
        return False

    def get_distance(self, img1, img2):
        # 计算两张小图的缺口距离
        # 比较两张图片的每一个像素点,误差不能超过某个阈值
        print(img1.size)
        left = 70
        # 遍历小图中横坐标58右边的所有点
        for x in range(left, img1.size[0]):
            for y in range(img1.size[1]):
                # 比较2张小图的像素点
                if not self.compare_img(img1, img2, x, y):
                    return x
        return left

    def slider_button(self, distance):
        # 拖动下面滑块
        slider = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div[6]/div/div[1]/div[2]/div[2]'))
        )
        action = ActionChains(self.browser)
        # 执行点击并抓住
        action.click_and_hold(slider).perform()
        print('==============')
        while distance > 0:
            print(distance)
            distance -= 2
            action.move_by_offset(xoffset=2, yoffset=0).perform()
            # 新建ActionChains对象防止累加位移
            action = ActionChains(self.browser)
            # time.sleep(0.2)
        action.release(slider).perform()

    def start(self):
        self.login_open()
        img_s1, img_s2 = self.screen_png()
        # 获取两张小图的距离 -- 从图中滑块的左边到阴影的左边
        distance = self.get_distance(img_s1, img_s2) - 7
        print(distance)
        # 滑动滑块
        self.slider_button(distance)


if __name__ == '__main__':
    # 扣有缺口图和没有缺口图,对比两张图的像素点,找出拖拽的横坐标,实现拖拽。
    spider = BiliSpider()
    spider.start()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容