前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >识别某多缺口滑块

识别某多缺口滑块

作者头像
小小咸鱼YwY
发布2021-07-23 16:20:04
1.2K0
发布2021-07-23 16:20:04
举报
文章被收录于专栏:python-爬虫python-爬虫
代码语言:javascript
复制
# _*_ 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")))
        utils.Utils.send_feishu_msg(f"点了三次还没点正确需要人工接入\n{url}",'6a93c5cf-85ef-4c03-aa14-8dde3f7b1d05')
    except:
        pass


if __name__ == '__main__':
    url ='https://m.pinduoduo.net/psnl_verification.html?VerifyAuthToken=n6g2qliU7MpQ5D4Ch2OBOAabdf7437fa31db92a'
    over_pdd_slider_certification(url)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-07-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档