VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • Python + Selenium 模拟登录jd

1. 前言

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

2. 环境

Python3.11.3 + selenium4.9.1 + opencv4.7 + PyAutoGUI0.9.54 + windows11

3. 开始

3.1 账号密码输入

进入登录页面,登录方式有两种,这里直接定位点击账号登录即可

# 进入登入页面
self.driver.get(self.config.login_url)
WebDriverWait(self.driver, 10).until(EC.url_to_be(self.config.login_url))
self.driver.maximize_window()
        
# 点击账号登录
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="login-tab login-tab-r"]/a')))
self.driver.find_element(By.XPATH, '//*[@class="login-tab login-tab-r"]/a').click()

# 账号密码输入
self.driver.find_element(By.ID, "loginname").send_keys(self.user_info.username)
self.driver.find_element(By.ID, "nloginpwd").send_keys(self.user_info.password)

3.2 通过验证码

3.2.1 验证码图片下载

看到验证码的图片是base64格式的,可以通过src属性来获取,然后直接转成cv图片格式即可

bigimg_b64 = self.driver.find_element(By.XPATH, '//*[@class="JDJRV-bigimg"]/img').get_attribute('src')
bigimg_data = base64.b64decode(bigimg_b64.replace('data:image/png;base64,', ''))
bigimg_array = np.frombuffer(bigimg_data, np.uint8)
bigimg_img = cv2.imdecode(bigimg_array, cv2.COLOR_RGB2BGR)

smallimg_b64 = self.driver.find_element(By.XPATH, '//*[@class="JDJRV-smallimg"]/img').get_attribute('src')
smallimg_data = base64.b64decode(smallimg_b64.replace('data:image/png;base64,', ''))
smallimg_array = np.frombuffer(smallimg_data, np.uint8)
smallimg_img = cv2.imdecode(smallimg_array, cv2.COLOR_RGB2BGR)

3.2.2 滑块需要移动的距离计算

这里可以用opencv来做,正确率还不错,而且还简单,直接把两张验证码图片经过灰度后,进行模板匹配即可,不过最后的结果还需要根据网页元素的尺寸进行调整

# 灰度化
bigimg_gray = cv2.cvtColor(bigimg_img, cv2.COLOR_BGR2GRAY)
smallimg_gray = cv2.cvtColor(smallimg_img, cv2.COLOR_BGR2GRAY)

# 模板匹配
result = cv2.matchTemplate(bigimg_gray, smallimg_gray, cv2.TM_CCOEFF_NORMED)
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)

# 移动距离对应到网页需要缩放(网页显示的图片和实际图片存在一定的比例差异)
x = minLoc[0] * (278.4 / 360.0)

3.2.3 定位滑动按钮

之前一直使用selenium的ActionChains来操作滑块按钮,但是一直通不过,应该是jd有针对selenium有检测,后面参考了网上可以使用PyAutoGUI来控制鼠标来滑动,参考链接,那就需要先定位到滑块的坐标,但是通过selenium获取的坐标还需要调整一下PyAutoGUI才能正确的定位到

WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@class="JDJRV-slide-inner JDJRV-slide-btn"]')))
slide_btn = self.driver.find_element(By.XPATH, '//*[@class="JDJRV-slide-inner JDJRV-slide-btn"]')
# TODO 网页元素位置映射到pyautogui会有一定缩放
offset_x = slide_btn.location.get('x') * 1.30
offset_y = slide_btn.location.get('y') * 1.75

3.2.4 模拟滑动

滑的时候发现上面opencv计算的移动距离还是有些偏差,还需要做些调整,而且滑动也得尽量拟人化,不然滑对了也通不过

# 直接滑到目标位置--会很难通过验证(用来调试移动距离是否正确)
# pyautogui.moveTo(offset_x,offset_y,duration=0.1 + random.uniform(0,0.1 + random.randint(1,100) / 100))
# pyautogui.mouseDown()
# pyautogui.moveTo(offset_x + x * 1.25, offset_y, duration=0.28)
# pyautogui.mouseUp()

# TODO 根据验证码原图计算的移动距离也需要调一下缩放
x = x * 1.25

# 鼠标移动到滑块
pyautogui.moveTo(offset_x,offset_y,duration=0.1 + random.uniform(0,0.1 + random.randint(1,100) / 100))
# 按下鼠标
pyautogui.mouseDown()
offset_y += random.randint(9,19)
# 开始滑动
pyautogui.moveTo(offset_x + int(x * random.randint(15,25) / 20),offset_y,duration=0.28)
offset_y += random.randint(-9,0)
pyautogui.moveTo(offset_x + int(x * random.randint(17,23) / 20),offset_y,
                         duration=random.randint(20,31) / 100)
offset_y += random.randint(0,8)
pyautogui.moveTo(offset_x + int(x * random.randint(19,21) / 20),offset_y,
                         duration=random.randint(20,40) / 100)
offset_y += random.randint(-3,3)
pyautogui.moveTo(x + offset_x + random.randint(-3,3),offset_y,duration=0.5 + random.randint(-10,10) / 100)
offset_y += random.randint(-2,2)
pyautogui.moveTo(x + offset_x + random.randint(-2,2),offset_y,duration=0.5 + random.randint(-3,3) / 100)
# 松开鼠标
pyautogui.mouseUp()

3.2.5 后续处理

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

4. 完整代码

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


处:https://www.cnblogs.com/qiu0000/p/17449590.html


相关教程