Commit b9e730ec authored by huangiyu's avatar huangiyu

1

parent 44ee201c
...@@ -144,35 +144,17 @@ class SlideUtils: ...@@ -144,35 +144,17 @@ class SlideUtils:
""" """
使用pyautogui实现滑块并自定义轨迹方程 使用pyautogui实现滑块并自定义轨迹方程
""" """
print(f"睡眠2秒后点击移动")
time.sleep(1)
xx = x + offset + offset_increase xx = x + offset + offset_increase
pyautogui.moveTo(x, y, duration=0.5) pyautogui.moveTo(x, y, duration=0.3)
pyautogui.mouseDown() pyautogui.mouseDown()
y += random.randint(9, 19) y += random.randint(9, 19)
pyautogui.moveTo(x + int(offset * random.randint(15, 23) / 20), y, duration=0.2) pyautogui.moveTo(x + int(offset * random.randint(15, 23) / 20), y, duration=0.5)
y += random.randint(-9, 0) y += random.randint(-9, 0)
pyautogui.moveTo(x + int(offset * random.randint(17, 21) / 20), y, duration=random.randint(20, 31) / 100) pyautogui.moveTo(x + int(offset * random.randint(17, 21) / 20), y, duration=random.randint(20, 31) / 100)
y += random.randint(-2, 2) pyautogui.moveTo(xx + random.randint(-2, 2), y, duration=0.8)
pyautogui.moveTo(xx, y, duration=0.3) duration=random.randint(50, 99) / 100
print("睡眠1秒后松开") print(duration)
time.sleep(0.5) pyautogui.moveRel(0, random.randint(2, 20), duration=duration)
import math
while_count = 0
while while_count < 3:
move_range = random.randint(10, 30)
while_count += 1
move_x = random.randint(-move_range, move_range)
# 生成随机的上下移动距离
move_y = random.randint(-int(move_range/2), int(move_range/2))
# 移动鼠标
pyautogui.moveRel(move_x, move_y, duration=0.2)
time.sleep(0.1) # 停顿一段时间
# 回到原始位置
pyautogui.moveTo(xx, y, duration=0.2)
time.sleep(0.5) # 停顿一段时间
print("松开") print("松开")
pyautogui.mouseUp() pyautogui.mouseUp()
# print(f"睡眠2秒后点击 # print(f"睡眠2秒后点击
...@@ -396,7 +378,8 @@ def get_cookie(): ...@@ -396,7 +378,8 @@ def get_cookie():
# query_spider_sql = f"SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE data_batch ='202401251550270001' and ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD' order by docID desc limit 10000" # query_spider_sql = f"SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE data_batch ='202401251550270001' and ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD' order by docID desc limit 10000"
# query_spider_sql = f"SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD' and data_batch ='202403051815460001' order by id" \ # query_spider_sql = f"SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD' and data_batch ='202403051815460001' order by id" \
# f" limit 100000" # f" limit 100000"
query_spider_sql = f"SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD' and project_id = '110' and (id like '%5' or id like '%6' or id like '%7' or id like '%8' or id like '%9' ) limit 100000" # query_spider_sql = f"-- SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD' and project_id = '41' and (id like '%5' or id like '%6' or id like '%7' or id like '%8' or id like '%9' ) limit 10000"
query_spider_sql = f"SELECT id,url,mallId FROM DMP_SEARCH_SPIDER WHERE data_batch ='202404180000000001' and ID NOT IN (SELECT ID FROM DMP_SEARCH_DATA) and mallId = 'DS-JD'"
query_spider = mysql_zdzs_proxy(query_spider_sql) query_spider = mysql_zdzs_proxy(query_spider_sql)
index = 0 index = 0
len_ = len(query_spider) len_ = len(query_spider)
...@@ -466,68 +449,62 @@ def get_cookie(): ...@@ -466,68 +449,62 @@ def get_cookie():
except: except:
print("点击“快速验证”按钮异常") print("点击“快速验证”按钮异常")
try: try:
WebDriverWait(driver, 2).until( WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="cpc_img"]')) EC.presence_of_element_located((By.XPATH, '//*[@id="cpc_img"]'))
) )
except: except:
print("未查找到验证图片背景图,可能是旋转图片,换一种方式获取div") print("未查找到验证图片背景图,可能是旋转图片,换一种方式获取div")
pass pass
background = driver.find_element(by=By.XPATH, value='//*[@id="cpc_img"]') while_count_2 = 0
background_url = background.get_attribute("src") while while_count_2 < 30 and (
# 用来找到登录图片的小滑块 '快速验证' in text and '验证一下,购物无忧' in text) or '前方拥挤,请刷新重试' in text or 'class="title">加载中...</span></div></div><!----></div>' in text:
slide = driver.find_element(by=By.XPATH, value='//*[@id="small_img"]') while_count_2 += 1
slide_url = slide.get_attribute("src") background = driver.find_element(by=By.XPATH, value='//*[@id="cpc_img"]')
background_img = 'background_img.png' background_url = background.get_attribute("src")
slide_img = 'slide_img.png' # 用来找到登录图片的小滑块
# 下载背景大图保存到本地 slide = driver.find_element(by=By.XPATH, value='//*[@id="small_img"]')
request.urlretrieve(background_url, background_img) slide_url = slide.get_attribute("src")
# 下载滑块保存到本地 background_img = 'background_img.png'
request.urlretrieve(slide_url, slide_img) slide_img = 'slide_img.png'
# 获取最佳x偏移量 # 下载背景大图保存到本地
x = SlideUtils.find_pic(background_img, slide_img) request.urlretrieve(background_url, background_img)
# print(f'本地最佳偏移量: {x}') # 下载滑块保存到本地
# 计算缩放 request.urlretrieve(slide_url, slide_img)
# 获取下载背景图宽度 # 获取最佳x偏移量
w1 = cv2.imread(background_img).shape[1] x = SlideUtils.find_pic(background_img, slide_img)
# 获取网页背景图宽度 # print(f'本地最佳偏移量: {x}')
w2 = background.size['width'] # 计算缩放
# 计算实际页面x偏移量 # 获取下载背景图宽度
x = (x * w2 / w1) w1 = cv2.imread(background_img).shape[1]
# 找到要获取位置的元素,比如通过其ID # 获取网页背景图宽度
element = driver.find_element_by_class_name("bg-blue") w2 = background.size['width']
# 计算实际页面x偏移量
# 获取元素的X和Y位置 x = (x * w2 / w1)
x_position = element.location['x'] + 50 # 找到要获取位置的元素,比如通过其ID
y_position = element.location['y'] + 100 element = driver.find_element_by_class_name("bg-blue")
print("元素的X位置:", x_position) # 获取元素的X和Y位置
print("元素的Y位置:", y_position) x_position = element.location['x'] + 50
# 其中x为屏幕左上角至滑块中心的横向像素值,y为屏幕左上角至滑块中心纵向像素值, 可根据自己屏幕配置 y_position = element.location['y'] + 100
# time.sleep(0.5)
SlideUtils.slide_by_pyautogui(x_position, y_position, print("元素的X位置:", x_position)
x, offset_increase) print("元素的Y位置:", y_position)
try: # 其中x为屏幕左上角至滑块中心的横向像素值,y为屏幕左上角至滑块中心纵向像素值, 可根据自己屏幕配置
WebDriverWait(driver, 2).until( # time.sleep(0.5)
EC.presence_of_element_located((By.ID, "J_main")) SlideUtils.slide_by_pyautogui(x_position, y_position,
) x, offset_increase)
text = driver.page_source
continue
except:
try: try:
driver.get(url) WebDriverWait(driver, 2).until(
WebDriverWait(driver, 5).until( EC.presence_of_element_located((By.ID, "J_main"))
EC.presence_of_element_located((By.CLASS_NAME, "verifyBtn"))
) )
except NoSuchElementException:
print("网页加载异常")
text = driver.page_source text = driver.page_source
continue continue
except:
print("没成功,重试")
finally: finally:
print("网页加载完成") print("网页加载完成")
finally: text = driver.page_source
print("网页加载完成") continue
text = driver.page_source
continue
except NoSuchElementException: except NoSuchElementException:
# print("未查找到验证图片背景图,可能是旋转图片,换一种方式获取div") # print("未查找到验证图片背景图,可能是旋转图片,换一种方式获取div")
try: try:
...@@ -650,7 +627,6 @@ def get_cookie(): ...@@ -650,7 +627,6 @@ def get_cookie():
cookie = '' cookie = ''
for i in driver.get_cookies(): for i in driver.get_cookies():
cookie = cookie + i['name'] + '=' + i['value'] + ';' cookie = cookie + i['name'] + '=' + i['value'] + ';'
driver.quit() driver.quit()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment