• 识别某多缺口滑块


    # _*_ coding: utf-8 _*_
    # @Time : 2021/6/28 2:48 下午
    # @Author : YwY(慕白)
    # @File : pdd_token.py
    import cv2
    import time
    
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    import random
    import base64
    from selenium.webdriver.support.wait import WebDriverWait
    
    import utils
    from utils.img_coordinate import base64_api
    from utils import Utils
    
    def get_stacks(distance):
        '''类人行为'''
        distance += 20
    
        '''
        匀加速减速运行
            v = v0 + a * t
    
        位移:
        s = v * t + 0.5 * a * (t**2)
        '''
    
        # 初速度
        v0 = 0
    
        # 加减速度列表
        a_list = [3, 4, 5]
    
        # 时间
        t = 0.2
    
        # 初始位置
        s = 0
    
        # 向前滑动轨迹
        forward_stacks = []
    
        mid = distance * 3 / 5
    
        while s < distance:
            if s < mid:
                a = a_list[random.randint(0, 2)]
    
            else:
                a = -a_list[random.randint(0, 2)]
    
            v = v0
    
            stack = v * t + 0.5 * a * (t ** 2)
    
            # 每次拿到的位移
            stack = round(stack)
    
            s += stack
    
            v0 = v + a * t
    
            forward_stacks.append(stack)
    
        back_stacks = [-1, -1, -2, -3, -2, -3, -2, -2, -3, -1]
    
        return {'forward_stacks': forward_stacks, 'back_stacks': back_stacks}
    
    
    def detect_displacement(img_slider_path, image_background_path):
        """detect displacement"""
        # # 参数0是灰度模式
        image = cv2.imread(img_slider_path, 0)
        template = cv2.imread(image_background_path, 0)
    
        size = image.shape
        # 寻找最佳匹配
        res = cv2.matchTemplate(_tran_canny(image), _tran_canny(template), cv2.TM_CCOEFF_NORMED)
        # 最小值,最大值,并得到最小值, 最大值的索引
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    
        top_left = max_loc[0]  # 横坐标
        # 展示圈出来的区域
        x, y = max_loc  # 获取x,y位置坐标
    
        w, h = image.shape[::-1]  # 宽高
        cv2.rectangle(template, (x, y), (x + w, y + h), (7, 249, 151), 2)
        return top_left
    
    def _tran_canny(image):
        """消除噪声"""
        image = cv2.GaussianBlur(image, (3, 3), 0)
        return cv2.Canny(image, 50, 150)
    
    
    def move_slide(driver):
        '''移动滑块'''
        slider_bs64 = driver.find_element_by_class_name("slider-img-bg").get_attribute("src")
        block_bs64 = driver.find_element_by_class_name("block-img").get_attribute("src")
        slider_content = base64.b64decode(slider_bs64.replace("data:image/jpeg;base64,", ''))
        with open("slider.png", "wb") as fw:
            fw.write(slider_content)
        block_content = base64.b64decode(block_bs64.replace("data:image/png;base64,", ''))
        with open("block.png", "wb") as fw:
            fw.write(block_content)
    
        top_left = detect_displacement("block.png", "slider.png") * 1.6
    
        # 类人行为
    
        stacks = get_stacks(top_left)
        forward_stacks = stacks['forward_stacks']
        back_stacks = stacks['back_stacks']
    
        slider_button = driver.find_element_by_id('slide-button')
        time.sleep(0.1)
    
        ActionChains(driver).click_and_hold(slider_button).perform()
        # time.sleep(0.1)
        # for forward_stack in forward_stacks:
        #     ActionChains(driver).move_by_offset(xoffset=forward_stack, yoffset=0).perform()
        #
        # for back_stack in back_stacks:
        #     ActionChains(driver).move_by_offset(xoffset=back_stack, yoffset=0).perform()
        #
        # time.sleep(0.1)
        #
        # ActionChains(driver).move_by_offset(xoffset=5, yoffset=0).perform()
        # ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform()
    
        ActionChains(driver).move_by_offset(xoffset=top_left, yoffset=0).perform()
        ActionChains(driver).release().perform()
    
    def move_slide(driver):
        '''移动滑块'''
        slider_bs64 = driver.find_element_by_class_name("slider-img-bg").get_attribute("src")
        block_bs64 = driver.find_element_by_class_name("block-img").get_attribute("src")
        slider_content = base64.b64decode(slider_bs64.replace("data:image/jpeg;base64,", ''))
        with open("slider.png", "wb") as fw:
            fw.write(slider_content)
        block_content = base64.b64decode(block_bs64.replace("data:image/png;base64,", ''))
        with open("block.png", "wb") as fw:
            fw.write(block_content)
    
        top_left = detect_displacement("block.png", "slider.png") * 1.6
    
        # 类人行为
    
        stacks = get_stacks(top_left)
        forward_stacks = stacks['forward_stacks']
        back_stacks = stacks['back_stacks']
    
        slider_button = driver.find_element_by_id('slide-button')
        time.sleep(0.1)
    
        ActionChains(driver).click_and_hold(slider_button).perform()
        # time.sleep(0.1)
        # for forward_stack in forward_stacks:
        #     ActionChains(driver).move_by_offset(xoffset=forward_stack, yoffset=0).perform()
        #
        # for back_stack in back_stacks:
        #     ActionChains(driver).move_by_offset(xoffset=back_stack, yoffset=0).perform()
        #
        # time.sleep(0.1)
        #
        # ActionChains(driver).move_by_offset(xoffset=5, yoffset=0).perform()
        # ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform()
    
        ActionChains(driver).move_by_offset(xoffset=top_left, yoffset=0).perform()
        ActionChains(driver).release().perform()
    
    
    def over_pdd_slider_certification(url):
        chrome_options = Options()
        # chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--disable-dev-shm-usage')
        # driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/Users/a3530/Desktop/搜索列表/crawlerBaokuan/chromedriver')
        driver = webdriver.Chrome(chrome_options=chrome_options)
        with open("./utils/stealth.min.js","r",encoding="utf8") as fr:
            chrome_js=  fr.read()
        # driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        #     "source": """
        #     Object.defineProperty(navigator, 'webdriver', {
        #       get: () => undefined
        #     })
        #   """
        # })
        driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{"source":chrome_js})
        driver.get(url)
        try:
            WebDriverWait(driver, 5, 1).until(EC.presence_of_element_located((By.CLASS_NAME, "intel-btn")))
            driver.find_element_by_class_name("intel-btn").click()
            time.sleep(2)
        except:
            pass
        status = 1
        while status==1:
            try:
                #presence_of_element_located 判断元素是否存在如果不存在会抛错
                time.sleep(3)
                WebDriverWait(driver, 5, 1).until(EC.presence_of_element_located((By.CLASS_NAME, "slider-img-bg")))
                move_slide(driver)
            except:
                status=2
        count = 0
        while count <=3:
            try:
                time.sleep(3)
                WebDriverWait(driver, 5, 1).until(EC.presence_of_element_located((By.CLASS_NAME, "picture-img")))
                picture_img = driver.find_element_by_class_name("picture-img")
                time.sleep(2)
                picture_text = driver.find_element_by_class_name("picture-text").text
                print(picture_text)
                picture_img_src = picture_img.get_attribute("src")
                ActionChains(driver).move_to_element(picture_img).perform()  #移动到图片中间
                ActionChains(driver).move_by_offset(-512/2,-257/2).perform()   #移动到左上角
                print(picture_img.size)
                picture_img_content = base64.b64decode(picture_img_src.replace("data:image/png;base64,", ''))
                with open("picture_img.png", "wb") as fw:
                    fw.write(picture_img_content)
                result = base64_api(uname='yjshitu', pwd='yjshitu2021', img="picture_img.png", typeid=19,content=picture_text)
                if isinstance(result,str):
                    try:
                        Utils.send_feishu_msg(result)
                    except:
                        pass
                    continue
                print("返回值")
                ActionChains(driver).move_by_offset(result[0],result[1]).click().perform()  # 移动到左上角
                print("已移动")
                count+=1
                print(f"当前次数-----{count}")
            except:
                break
        try:
            WebDriverWait(driver, 5, 1).until(EC.presence_of_element_located((By.CLASS_NAME, "picture-img")))
    
        except:
            pass
    
    
    if __name__ == '__main__':
        url ='https://m.pinduoduo.net/psnl_verification.html?VerifyAuthToken=n6g2qliU7MpQ5D4Ch2OBOAabdf7437fa31db92a'
        over_pdd_slider_certification(url)
    
  • 相关阅读:
    推荐系统-01-简单逻辑回归
    顶部BANNER
    大数据-12-Spark+Kafka构建实时分析Dashboard
    大数据-10-Spark入门之支持向量机SVM分类器
    大数据-11-案例演习-淘宝双11数据分析与预测
    大数据-09-Intellij idea 开发java程序操作HDFS
    大数据-08-Sqoop入门
    大数据-07-Spark之流数据
    准确度,精确度, 召回率
    [转]springcloud(九):配置中心和消息总线(配置中心终结版)
  • 原文地址:https://www.cnblogs.com/pythonywy/p/15038154.html
Copyright © 2020-2023  润新知