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()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容