Python?+?Selenium?實現(xiàn)模擬登錄jd實例分享
1. 前言
最近有點時間,就隨便找點東西弄弄,倒也碰到了一些問題,在此記錄下
2. 環(huán)境
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來做,正確率還不錯,而且還簡單,直接把兩張驗證碼圖片經(jīng)過灰度后,進行模板匹配即可,不過最后的結果還需要根據(jù)網(wǎng)頁元素的尺寸進行調(diào)整
# 灰度化 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) # 移動距離對應到網(wǎng)頁需要縮放(網(wǎng)頁顯示的圖片和實際圖片存在一定的比例差異) x = minLoc[0] * (278.4 / 360.0)
3.2.3 定位滑動按鈕
之前一直使用selenium的ActionChains來操作滑塊按鈕,但是一直通不過,應該是jd有針對selenium有檢測,后面參考了網(wǎng)上可以使用PyAutoGUI來控制鼠標來滑動,那就需要先定位到滑塊的坐標,但是通過selenium獲取的坐標還需要調(diào)整一下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 網(wǎng)頁元素位置映射到pyautogui會有一定縮放
offset_x = slide_btn.location.get('x') * 1.30
offset_y = slide_btn.location.get('y') * 1.753.2.4 模擬滑動
滑的時候發(fā)現(xiàn)上面opencv計算的移動距離還是有些偏差,還需要做些調(diào)整,而且滑動也得盡量擬人化,不然滑對了也通不過
# 直接滑到目標位置--會很難通過驗證(用來調(diào)試移動距離是否正確)
# 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 根據(jù)驗證碼原圖計算的移動距離也需要調(diào)一下縮放
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 后續(xù)處理
到此基本上模擬登陸就完成了,避免失敗,可以加個循環(huán),滑塊未通過時繼續(xù)下一張,再做一些是否登錄成功的驗證就歐克啦。
4. 完整代碼
https://github.com/QiuMiMi/Get-jd
到此這篇關于Python + Selenium 實現(xiàn)模擬登錄jd實例分享的文章就介紹到這了,更多相關Python + Selenium 模擬登錄jd內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python selenium 三種等待方式詳解(必會)
- python selenium 獲取標簽的屬性值、內(nèi)容、狀態(tài)方法
- 玩轉python selenium鼠標鍵盤操作(ActionChains)
- Python + selenium自動化環(huán)境搭建的完整步驟
- SpringBoot優(yōu)化啟動速度的方法實現(xiàn)
- Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能
- Python中使用 Selenium 實現(xiàn)網(wǎng)頁截圖實例
- Python selenium文件上傳方法匯總
- Python selenium 三種等待方式解讀
- python+selenium 定位到元素,無法點擊的解決方法
相關文章
pandas常用表連接merge/concat/join/append詳解
使用python的pandas庫可以很容易幫你搞定,而且性能也是很出色的;百萬級的表關聯(lián),可以秒出,本文給大家分享pandas常用表連接merge/concat/join/append詳解,感興趣的朋友跟隨小編一起看看吧2023-02-02

