B占图片验证登录-2019-11-4

失败原因有可能是和电脑缩放比例有关,本人缩放的是120%
需要看selenium的ActionChains这个包使用步骤,详细教程可参https://blog.csdn.net/wudaoshihun/article/details/82990670
import time
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

USERNAME = '用户名'
PASSWORD = '密码'


class BliBliSpider(object):
    def __init__(self):
        self.driver = webdriver.Chrome()

    def get_track(self, s):
        """获取移动列表"""
        track = []
        for t in range(36):
            if t < 21:
                t = t / 20
                a1 = 8 * s / 5
                v0 = a1 * t
                t = 0.05
                s1 = v0 * t + 0.5 * a1 * t * t
                track.append(s1)
            else:
                a2 = -27 * s / 8
                v0 = 8 * s / 5 + a2 * (-20 + t) / 20
                t = 0.05
                s2 = v0 * t + 0.5 * a2 * t * t
                track.append(s2)
        return track

    def move(self, crave):
        time.sleep(2)
        js = 'document.querySelectorAll("canvas")[3].style.display="none"'
        self.driver.execute_script(js)
        s = 0
        j = 0
        track = self.get_track(crave)
        if crave < 100:
            track.extend([-3, 2.5, 1.5, -1.5, 1.5, -2.5])
        else:
            track.extend([-3, 4.1, -4.1, 1.5, -1.5])
        for i in track:
            print(i)
            j += 1
            if j > 36:
                pass
            # 利用ActionChains 这个模块,获取移动模块的标签,移动x轴
            ActionChains(self.driver).click_and_hold(
                on_element=self.driver.find_element_by_class_name("geetest_slider_button")).move_by_offset(
                xoffset=i, yoffset=0).perform()
            time.sleep(0.05)
            s += i
        print('移动:', s)
        # 移动完毕,松开鼠标
        ActionChains(self.driver).release(
            on_element=self.driver.find_element_by_class_name("geetest_slider_button")).perform()
        time.sleep(2)

    def difference(self, img1, img2, x, y):
        """两张图片每个像素点对比,差值过大的为拼图位置"""
        p1 = img1.load()[x, y]
        p2 = img2.load()[x, y]
        p = 60
        # print('p1:',p1,'\np2:',p2)
        if abs(p1[0] - p2[0]) < p and abs(p1[1] - p2[1]) < p and abs(p1[2] - p2[2]) < p:
            return True
        else:
            print(p1, p2)
            return False

    def get_crave(self, img1, img2):
        ahead = 80
        for i in range(ahead, img1.size[0]):
            for j in range(img1.size[1]):
                if not self.difference(img1, img2, i, j):
                    print('第%s列' % i)
                    crave = i
                    return crave

    def login(self):
        self.driver.get("https://passport.bilibili.com/login")
        # self.driver.maximize_window()
        self.driver.find_element_by_id('login-username').send_keys(USERNAME)
        self.driver.find_element_by_id('login-passwd').send_keys(PASSWORD)
        self.driver.find_element_by_xpath('//a[contains(text(), "登录")]').click()
        time.sleep(2)
        # 获取有缺口标签
        self.driver.save_screenshot('button1.png')
        left = 795
        top = 330
        right = left + 300
        bottom = top + 180
        im = Image.open('button1.png')
        im1 = im.crop((left, top, right, bottom))
        im1.save('button1.png')
        # 添加休眠时间,执行js代码
        time.sleep(2)
        js = 'document.querySelectorAll("canvas")[3].style.display="block"'
        self.driver.execute_script(js)
        # 获取无缺口标签
        self.driver.save_screenshot('button.png')
        left = 795
        top = 330
        right = left + 300
        bottom = top + 180
        im = Image.open('button.png')

        im2 = im.crop((left, top, right, bottom))
        im2.save('button.png')
        crave = self.get_crave(im1, im2) - 10
        if crave < 85:
            self.driver.quit()
            return crave + 10
        if crave < 135:
            crave += 4
        if crave > 135 and crave < 189:
            crave -= 5
        if crave >= 190:
            crave -= 19
        print('carve:', crave)
        self.move(crave)

    def close(self):
        self.driver.close()


if __name__ == '__main__':
    while True:
        try:
            BiBi = BliBliSpider()
            BiBi.login()
        except Exception as e:
            pass

大概测试后,成功率有60%以上,后期还需要优化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值