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, '//*[@]/a')))
self.driver.find_element(By.XPATH, '//*[@]/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, '//*[@]/img').get_attribute('src')
bigimg_data = https://www.cnblogs.com/qiu0000/archive/2023/06/01/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, '//*[@]/img').get_attribute('src')
smallimg_data = https://www.cnblogs.com/qiu0000/archive/2023/06/01/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, '//*[@]')))
slide_btn = self.driver.find_element(By.XPATH, '//*[@]')
# 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.uj5u.com/houduan/554060.html
標籤:其他
上一篇:EasyExcel
下一篇:返回列表
