識別某多缺口滑塊

# _*_ 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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章