翼度科技»论坛 编程开发 python 查看内容

Python + Selenium 模拟登录jd

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
1. 前言

最近有点时间,就随便找点东西弄弄,倒也碰到了一些问题,在此记录下
2. 环境

Python3.11.3 + selenium4.9.1 + opencv4.7 + PyAutoGUI0.9.54 + windows11
3. 开始

3.1 账号密码输入


进入登录页面,登录方式有两种,这里直接定位点击账号登录即可
  1. # 进入登入页面
  2. self.driver.get(self.config.login_url)
  3. WebDriverWait(self.driver, 10).until(EC.url_to_be(self.config.login_url))
  4. self.driver.maximize_window()
  5.         
  6. # 点击账号登录
  7. WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@]/a')))
  8. self.driver.find_element(By.XPATH, '//*[@]/a').click()
  9. # 账号密码输入
  10. self.driver.find_element(By.ID, "loginname").send_keys(self.user_info.username)
  11. self.driver.find_element(By.ID, "nloginpwd").send_keys(self.user_info.password)
复制代码
3.2 通过验证码


3.2.1 验证码图片下载

看到验证码的图片是base64格式的,可以通过src属性来获取,然后直接转成cv图片格式即可
  1. bigimg_b64 = self.driver.find_element(By.XPATH, '//*[@]/img').get_attribute('src')
  2. bigimg_data = base64.b64decode(bigimg_b64.replace('data:image/png;base64,', ''))
  3. bigimg_array = np.frombuffer(bigimg_data, np.uint8)
  4. bigimg_img = cv2.imdecode(bigimg_array, cv2.COLOR_RGB2BGR)
  5. smallimg_b64 = self.driver.find_element(By.XPATH, '//*[@]/img').get_attribute('src')
  6. smallimg_data = base64.b64decode(smallimg_b64.replace('data:image/png;base64,', ''))
  7. smallimg_array = np.frombuffer(smallimg_data, np.uint8)
  8. smallimg_img = cv2.imdecode(smallimg_array, cv2.COLOR_RGB2BGR)
复制代码
3.2.2 滑块需要移动的距离计算





这里可以用opencv来做,正确率还不错,而且还简单,直接把两张验证码图片经过灰度后,进行模板匹配即可,不过最后的结果还需要根据网页元素的尺寸进行调整
  1. # 灰度化
  2. bigimg_gray = cv2.cvtColor(bigimg_img, cv2.COLOR_BGR2GRAY)
  3. smallimg_gray = cv2.cvtColor(smallimg_img, cv2.COLOR_BGR2GRAY)
  4. # 模板匹配
  5. result = cv2.matchTemplate(bigimg_gray, smallimg_gray, cv2.TM_CCOEFF_NORMED)
  6. minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
  7. # 移动距离对应到网页需要缩放(网页显示的图片和实际图片存在一定的比例差异)
  8. x = minLoc[0] * (278.4 / 360.0)
复制代码
3.2.3 定位滑动按钮

之前一直使用selenium的ActionChains来操作滑块按钮,但是一直通不过,应该是jd有针对selenium有检测,后面参考了网上可以使用PyAutoGUI来控制鼠标来滑动,参考链接,那就需要先定位到滑块的坐标,但是通过selenium获取的坐标还需要调整一下PyAutoGUI才能正确的定位到
  1. WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@]')))
  2. slide_btn = self.driver.find_element(By.XPATH, '//*[@]')
  3. # TODO 网页元素位置映射到pyautogui会有一定缩放
  4. offset_x = slide_btn.location.get('x') * 1.30
  5. offset_y = slide_btn.location.get('y') * 1.75
复制代码
3.2.4 模拟滑动

滑的时候发现上面opencv计算的移动距离还是有些偏差,还需要做些调整,而且滑动也得尽量拟人化,不然滑对了也通不过
  1. # 直接滑到目标位置--会很难通过验证(用来调试移动距离是否正确)
  2. # pyautogui.moveTo(offset_x,offset_y,duration=0.1 + random.uniform(0,0.1 + random.randint(1,100) / 100))
  3. # pyautogui.mouseDown()
  4. # pyautogui.moveTo(offset_x + x * 1.25, offset_y, duration=0.28)
  5. # pyautogui.mouseUp()
  6. # TODO 根据验证码原图计算的移动距离也需要调一下缩放
  7. x = x * 1.25
  8. # 鼠标移动到滑块
  9. pyautogui.moveTo(offset_x,offset_y,duration=0.1 + random.uniform(0,0.1 + random.randint(1,100) / 100))
  10. # 按下鼠标
  11. pyautogui.mouseDown()
  12. offset_y += random.randint(9,19)
  13. # 开始滑动
  14. pyautogui.moveTo(offset_x + int(x * random.randint(15,25) / 20),offset_y,duration=0.28)
  15. offset_y += random.randint(-9,0)
  16. pyautogui.moveTo(offset_x + int(x * random.randint(17,23) / 20),offset_y,
  17.                          duration=random.randint(20,31) / 100)
  18. offset_y += random.randint(0,8)
  19. pyautogui.moveTo(offset_x + int(x * random.randint(19,21) / 20),offset_y,
  20.                          duration=random.randint(20,40) / 100)
  21. offset_y += random.randint(-3,3)
  22. pyautogui.moveTo(x + offset_x + random.randint(-3,3),offset_y,duration=0.5 + random.randint(-10,10) / 100)
  23. offset_y += random.randint(-2,2)
  24. pyautogui.moveTo(x + offset_x + random.randint(-2,2),offset_y,duration=0.5 + random.randint(-3,3) / 100)
  25. # 松开鼠标
  26. pyautogui.mouseUp()
复制代码
3.2.5 后续处理

到此基本上模拟登陆就完成了,避免失败,可以加个循环,滑块未通过时继续下一张,再做一些是否登录成功的验证就欧克啦。
4. 完整代码

https://github.com/QiuMiMi/Get-jd

来源:https://www.cnblogs.com/qiu0000/p/17449590.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具