之前接觸的的驗證碼都是圖形驗證碼,可以借助圖片識別來實現,不過識別率不太高
后又研究繞過驗證碼實現登錄。
最近有遇到短信驗證碼的問題,網上看了有各種處理方法。
1、cookie 登錄
自己先嘗試了,使用cookie 登錄的方法,
具體詳見代碼
1、 獲取cookie
# FileName : VerifyCodeLogin.py # Author : Adil # DateTime : 2018/4/9 13:09 # SoftWare : PyCharm from selenium import webdriver import time,os,yaml url = 'https://www.baidu.com/' driver = webdriver.Chrome() driver.get(url) driver.implicitly_wait(5) driver.maximize_window() # 打開登錄窗口 driver.find_element_by_xpath('//*[@id="u1"]/a[7]').click() # 選擇用戶名密碼 driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]').click() # 選擇短信驗證碼登錄 driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__smsSwitchWrapper"]').click() cookiesList = driver.get_cookies() print(cookiesList) # 輸入手機號 driver.find_element_by_id('TANGRAM__PSP_10__smsPhone').clear() driver.find_element_by_id('TANGRAM__PSP_10__smsPhone').send_keys("手機號碼") # 發送驗證碼 driver.find_element_by_id('TANGRAM__PSP_10__smsTimer').click() # 輸入驗證碼 print("請輸入驗證碼:") verifyCode = input() driver.find_element_by_id('TANGRAM__PSP_10__smsVerifyCode').clear() driver.find_element_by_id('TANGRAM__PSP_10__smsVerifyCode').send_keys(verifyCode) driver.find_element_by_id('TANGRAM__PSP_10__smsSubmit').click() time.sleep(5) # 獲取登錄后的cookies loginCookies = driver.get_cookies() print(loginCookies) # 存儲cookie # cookie1 = loginCookies[5] # cookie2 = loginCookies[6] # cookie3 = loginCookies[8] cookies = cookiesList # 存儲到本地yaml 文件 # 當前文件路徑 yamldir = os.path.split(os.path.realpath(__file__))[0] yamlFileName = os.path.join(yamldir,'cookies.yaml') fw = open(yamlFileName,'w',encoding='utf-8') # 數據組裝 data = {"cookies":loginCookies} # 數據裝載 yaml.dump(data,fw) driver.quit()
2、使用cookie 登錄
# FileName : CookieLogin.py # Author : Adil # DateTime : 2018/4/9 13:35 # SoftWare : PyCharm from selenium import webdriver import time,os,yaml url = 'https://www.baidu.com/' driver = webdriver.Chrome() driver.get(url) driver.implicitly_wait(5) driver.maximize_window() driver.delete_all_cookies() print(driver.get_cookies()) fileDir = os.path.split(os.path.realpath(__file__))[0] yamlFileName = os.path.join(fileDir,'cookies.yaml') # 讀取yaml 文件 f = open(yamlFileName,'r',encoding='utf-8') cont = f.read() cookies = yaml.load(cont) cookiesList = cookies.get('cookies') print(cookiesList) for cookie in cookiesList: print(cookie) driver.add_cookie(cookie) print(driver.get_cookies()) time.sleep(5) driver.get(url) driver.refresh() time.sleep(15) driver.quit()
2、使用萬能驗證碼
讓研發提供一個萬能驗證碼
3、驗證碼存到數據庫中,從數據庫中讀取驗證碼
4、做一個短信廣播實時監聽短信驗證碼
這里需要做一個短信廣播小程序
參考地址:https://www.cnblogs.com/itstu/p/6873221.html
http://tieba.baidu.com/p/5153637250?red_tag=z1616417748