Explorar el Código

fix: 优化滑块滑动策略

wuwenyi hace 1 mes
padre
commit
12e19ca20b
Se han modificado 2 ficheros con 16 adiciones y 6 borrados
  1. 12 0
      README.md
  2. 4 6
      xhs/rotate_ident.py

+ 12 - 0
README.md

@@ -12,4 +12,16 @@ browser = huitun.HuiTunBrowser('13417350072')
 browser.__init_browser__()
 page = browser.page
 page.goto(huitun.HUITUN_URL)
+```
+
+滑块验证 test
+```python
+from playwright.sync_api import sync_playwright, Page, Playwright
+import xhs
+browser = xhs.XhsBrowser('18565121135')
+browser.__init_browser__()
+page = browser.page
+page.goto('https://www.xiaohongshu.com/website-login/captcha?redirectPath=https%3A%2F%2Fwww.xiaohongshu.com%2Fexplore%2F66b095ea0000000025033d76%3FexSource%3D&verifyUuid=5a2fcb9e-b2cd-4813-8647-f50b5add9fe5*DJnYZiom&verifyType=101&verifyBiz=461')
+rotate_obj = xhs.RotateIdent(page)
+rotate_obj.handle_rotate()
 ```

+ 4 - 6
xhs/rotate_ident.py

@@ -85,7 +85,7 @@ class RotateIdent:
         处理旋转验证码的核心方法
         """
         try_count = 0
-        while self.need_ident() and try_count < 5:
+        while self.need_ident() and try_count < 4:
             try_count += 1
             logging.info('开始处理旋转验证码,第 %s 次', try_count)
             tag2 = self.page.query_selector('//div[@class="red-captcha-slider"]')
@@ -94,19 +94,17 @@ class RotateIdent:
             if response['code'] != 200:
                 logging.error(response['msg'])
             else:
-                angle = int(str(response['data']['res_str']).replace('顺时针旋转', '').replace('度', ''))
+                angle = response['data']['angle']
                 # 使用鼠标操作进行点击并保持
                 bbox = tag2.bounding_box()
                 x_center = bbox['x'] + bbox['width'] / 2
                 y_center = bbox['y'] + bbox['height'] / 2
                 self.page.mouse.move(x_center, y_center)
                 self.page.mouse.down()
-                # 等待一段时间
-                time.sleep(1)
                 # 计算实际滑动距离 = 像素距离 + 前面空白距离
                 move_x = angle * 0.79
-                # 滑动鼠标
-                self.page.mouse.move(x_center + move_x, y_center + 5)
+                # 滑动滑块,且坐标多增加一个像素,添加误差
+                self.page.mouse.move(x_center + move_x + 1, y_center + 5, steps=10)
                 # 等待一段时间
                 time.sleep(1)
                 # 释放鼠标按钮