关于python3网络爬虫开发实战,极验 滑索的遇到的问题

我不知道是不是因为电脑屏幕尺寸不同或者是其他什么原因,我按照催老师的源码发现,截图的位置出错。每次都是得到全白的图片。后来发现截图下来的图片和真实网页页面比例有问题,所以最后用土办法手动测量 。 关于python3网络爬虫开发实战,极验 滑索的遇到的问题_第1张图片
希望可以帮到和我情况一样的朋友
class CrackGeetest():
    def __init__(self):
        self.url='https://account.geetest.com/login'
        self.browser=webdriver.Chrome()
        self.wait=WebDriverWait(self.browser,20)
        self.email=email
        self.password=password
    def __del__(self):
        self.browser.close()
    def get_geetest_button(self):
        
        button=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'geetest_radar_tip')))
        return button 
    def get_position(self):
        img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_img')))
        time.sleep(2)
        location = img.location
        print(location)
        size = img.size
        top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[

            'width']
        return (top, bottom, left, right)
    
    def get_screenshot(self):
        screenshot = self.browser.get_screenshot_as_png()
        screenshot = Image.open(BytesIO(screenshot))
        #screenshot.show()
        return screenshot
    def get_slider(self):
        slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_slider_button')))
        return slider
    def get_geetest_image(self, name='captcha.png'):
        top, bottom, left, right = self.get_position()
        print('验证码位置',  top, bottom, left,right)
        screenshot = self.get_screenshot()
        X=900
        Y=350
        captcha = screenshot.crop((X, Y, X+450, Y+320))
        print(captcha.size)
        #captcha.show()
        captcha.save(name)
        return captcha
    def get_gap(self, im3):
        a=im3.size[0]
        b=im3.size[1]
        left = 120
        go=0
        for i in range(left, a):
            for j in range(b):
                if self.is_pixel_equal(im3, i, j):
                    go=i
                    return go
        return go
        
    def open(self):
        self.browser.get(self.url)
        email = self.wait.until(EC.presence_of_element_located((By.ID, 'email')))
        password = self.wait.until(EC.presence_of_element_located((By.ID, 'password')))
        email.send_keys(self.email)
        password.send_keys(self.password)
    def is_pixel_equal(self, im3, x, y):
        
        pixel = im3.load()[x, y]
        threshold = 50
        if pixel[0]>threshold and pixel[1]>threshold and pixel[1]>threshold:

            return True
        else:
            return False
    def get_track(self, distance):
        track = []
        current = 0
        mid = distance * 4 / 5
        t = 0.2
        v = 0
        while current < distance:
            if current < mid:
                a = 2
            else:
                a = -3
            v0 = v
            v = v0 + a * t
            move = v0 * t + 1 / 2 * a * t * t
            current += move
            track.append(round(move))
        return track
    def move_to_gap(self, slider, track):

        ActionChains(self.browser).click_and_hold(slider).perform()
        for x in track:
            ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
        time.sleep(0.5)
        ActionChains(self.browser).release().perform()
    def login(self):
        submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'login-btn')))
        submit.click()
        time.sleep(10)
        print('登录成功')
    def crack(self):
        self.open()
        button = self.get_geetest_button()
        button.click()
        image1 = self.get_geetest_image('captcha1.png')
        slider = self.get_slider()
        slider.click()
        time.sleep(0.5)
        image2 = self.get_geetest_image('captcha2.png')
        im3=ImageChops.difference(image1,image2)#这边做了小小的修改,精度更加高了
        #im3.show()
        gap=self.get_gap(im3)
        print('缺口位置', gap)
        #gap -= BORDER
        gap=round(gap*0.48)#手量的比例差,
        track = self.get_track(gap)
        print('滑动轨迹', track)
        self.move_to_gap(slider, track)
        success = self.wait.until(
            EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功'))
        print(success)
        if not success:
            self.crack()
        else:
            self.login()
if __name__ == '__main__':

    crack = CrackGeetest()

    crack.crack()
    

你可能感兴趣的:(关于python3网络爬虫开发实战,极验 滑索的遇到的问题)