python如何破解绕开滑动拼图验证码?
网友回复
使用pyppeteer获取滑块图片并通过cv2来进行图片处理算出拼凑位置的像素值,最后模拟用户进行滑动完成验证码,完整的代码如下:
#!/usr/local/python3/bin/python3 # -*- coding: utf-8 -* from pyppeteer import launch import asyncio import time import json import random import cv2 def get_track(length): list = [] x = random.randint(1,10) while length: list.append(x) length -= x if length > 0 and length <= 5: break elif 5 < length < 25: x = random.randint(2,length) else: x = random.randint(5,25) for i in range(length): list.append(1) return list def change_size( file): image = cv2.imread(file, 1) # 读取图片 image_name应该是变量 img = cv2.medianBlur(image, 5) # 中值滤波,去除黑色边际中可能含有的噪声干扰 b = cv2.threshold(img, 15, 255, cv2.THRESH_BINARY) # 调整裁剪效果 binary_image = b[1] # 二值图--具有三通道 binary_image = cv2.cvtColor(binary_image, cv2.COLOR_BGR2GRAY) x, y = binary_image.shape edges_x = [] edges_y = [] for i in range(x): for j in range(y): if binary_image[j] == 255: edges_x.append(i) edges_y.append(j) left = min(edges_x) # 左边界 right = max(edges_x) # 右边界 width = right - left # 宽度 bottom = min(edges_y) # 底部 top = max(edges_y) # 顶部 height = top - bottom # 高度 pre1_picture = image[left:left + width, bottom:bottom + height] # 图片截取 return pre1_picture # 返回图片数据 async def main(url): try: browser = await launch({'headless':False, 'dumpio': True, 'autoClose': False, 'args': ['--no-sandbox', '--window-size=1366,850']}) page = await browser.newPage() await page.evaluateOnNewDocument('''() => {delete navigator.__proto__.webdriver;}''') await page.evaluateOnNewDocument('''() => {Object.defineProperty(navigator, 'webdriver', {get: () => undefined,});}''') await page.evaluateOnNewDocument('''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-CN', 'cn'] }); }''') ...
点击查看剩余70%
我们以b站登录为例
#!/usr/local/python3/bin/python3 # -*- coding: utf-8 -* # -*- coding:utf-8 -*- from PIL import Image from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.desired_capabilities import DesiredCapabilities headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" } chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option('w3c', False) caps = DesiredCapabilities.CHROME caps['loggingPrefs'] = {'performance': 'ALL'} class SliderVerificationCode(object): def __init__(self): # 初始化一些信息 self.left = 60 # 定义一个左边的起点 缺口一般离图片左侧有一定的距离 有一个滑块 self.url = 'https://passport.bilibili.com/login' self.driver = webdriver.Chrome(desired_capabilities=caps,options=chrome_options) self.wait = WebDriverWait(self.driver, 20) # 设置等待时间20秒 self.phone = "18888888888" self.passwd = "12334545" def input_name_password(self): # 输入账号密码 self.driver.get(self.url) self.driver.maximize_window() input_name = self.driver.find_element_by_xpath("//input[@id='login-username']") input_pwd = self.driver.find_element_by_xpath("//input[@id='login-passwd']") input_name.send_keys("username") input_pwd.send_keys("passport") sleep(3) def click_login_button(self): #...
点击查看剩余70%