当前位置:网站首页>Selenium + phantom JS crack sliding verification 2

Selenium + phantom JS crack sliding verification 2

2022-04-23 17:58:00 Feng Ye 520

from selenium import webdriver
from PIL import Image
from io import BytesIO
import re
import requests
import numpy as np
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from random import randint
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
    )


class Bilibili:
    def __init__(self):
        self.broswer = webdriver.PhantomJS(executable_path=r'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe',
                                           desired_capabilities=dcap)
        self.broswer.set_page_load_timeout(20)
        self.broswer.implicitly_wait(10)
    
    def get_page(self):
        self.broswer.get('https://passport.bilibili.com/login')
        name = self.broswer.find_element_by_id('login-username')
        name.send_keys('usename')
        passwd = self.broswer.find_element_by_id('login-passwd')
        passwd.send_keys('passwd')
            
    def get_img(self):
       
#        pattern = re.compile(r'background-image: url\(\"([^\']+)\"\); background-position: ([^p]+)px ([^p]+)px;')
        pattern = re.compile(r'background-image: url\(([^\']+)\); background-position: ([^p]+)px ([^p]+)px;')
        img1 = self.broswer.find_element_by_class_name('gt_cut_bg_slice').get_attribute('style')
        img2 = self.broswer.find_element_by_class_name('gt_cut_fullbg_slice').get_attribute('style')
        
  
        img1_url = pattern.findall(img1)[0][0]
        
        img2_url = pattern.findall(img2)[0][0]
        img1_url = img1_url.replace('webp','jpg')
        img2_url = img2_url.replace('webp','jpg')
        
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"}
        request_img1 = requests.get(img1_url,headers = headers).content
        img1_content = Image.open(BytesIO(request_img1))
        
        request_img2 = requests.get(img2_url,headers = headers).content
        img2_content = Image.open(BytesIO(request_img2))
        
        return img1_content,img2_content
    
    def crop_img(self):
        img1_content,img2_content = self.get_img()
#        pattern = re.compile(r'background-image: url\(\"([^\']+)\"\); background-position: ([^p]+)px ([^p]+)px;')
        pattern = re.compile(r'background-image: url\(([^\']+)\); background-position: ([^p]+)px ([^p]+)px;')
        imgs1 = self.broswer.find_elements_by_class_name('gt_cut_bg_slice')
        imgs2 = self.broswer.find_elements_by_class_name('gt_cut_fullbg_slice')
        img1_coord_list = []
        img2_coord_list = []
        
        for img1 in imgs1:
            img1_x = int(pattern.findall(img1.get_attribute('style'))[0][1])
            img1_y = int(pattern.findall(img1.get_attribute('style'))[0][2])
            img1_coord_list.append([img1_x,img1_y])

        for img2 in imgs2:
            img2_x = int(pattern.findall(img2.get_attribute('style'))[0][1])
            img2_y = int(pattern.findall(img2.get_attribute('style'))[0][2])
            img2_coord_list.append([img2_x,img2_y])
        
  
        new_img1 = Image.new('RGB',(260,116))
        new_img2 = Image.new('RGB',(260,116))
        offset_x_down = 0
        offset_x_upper = 0
        for img1_x,img1_y in img1_coord_list:
            if img1_y == -58:
                img1_crop = img1_content.crop((abs(img1_x),58,abs(img1_x)+10,116))
                new_img1.paste(img1_crop,(offset_x_upper,0))
                offset_x_upper+=10
            elif img1_y == 0:
                img1_crop = img1_content.crop((abs(img1_x),0,abs(img1_x)+10,58))
                new_img1.paste(img1_crop,(offset_x_down,58))
                offset_x_down+=10
        
        offset_x_down = 0
        offset_x_upper = 0
        for img2_x,img2_y in img2_coord_list:
            if img2_y == -58:
                img2_crop = img2_content.crop((abs(img2_x),58,abs(img2_x)+10,116))
                new_img2.paste(img2_crop,(offset_x_upper,0))
                offset_x_upper+=10
            elif img2_y == 0:
                img2_crop = img2_content.crop((abs(img2_x),0,abs(img2_x)+10,58))
                new_img2.paste(img2_crop,(offset_x_down,58))
                offset_x_down+=10
            
        return new_img1,new_img2
        
    def diff_img(self):
        new_img1,new_img2 = self.crop_img()

        for x in range(new_img1.size[0]):
            for y in range(new_img1.size[1]):
                px1 = new_img1.getpixel((x, y)) 
                px2 = new_img2.getpixel((x, y)) 
                for i in range(3):
                    if abs(px1[i]-px2[i])>=50:
                        return x, y
        
    def track(self):
        length = self.diff_img()[0]-6
        length_1 = int(length*0.7)
        length_2 = length-length_1
        button = self.broswer.find_element_by_xpath('//*[@id="gc-box"]/div/div[3]/div[2]')
        ActionChains(self.broswer).click_and_hold(on_element=button).perform()
        
        for i in range(length_1):
            ActionChains(self.broswer).move_by_offset(1,0).perform()
            sleep(randint(0,20)/1000)
        for i in range(length_2):
            ActionChains(self.broswer).move_by_offset(1,0).perform()
            sleep(randint(30,50)/1000)
        
        ActionChains(self.broswer).release(on_element=button).perform()
        sleep(2)
        result = self.broswer.find_element_by_class_name('gt_info_type')

#        print(result.text)
        return result.text
    
    def action(self):
        self.get_page()

        while True: 
            result = self.track()
            if u' Again ' in result:
                print(result)
                sleep(4)
                continue
            elif u' adopt ' in result:
                print(result)
                break
            elif u' Failure ' in result:
                self.broswer.execute_script('location.reload()')
                print(' Refresh the page ')
                sleep(4)
            else:
                print(result)
                break
           

b = Bilibili()
b.action()
 

版权声明
本文为[Feng Ye 520]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230546134259.html