Python超強爬蟲8天速成(完整版)爬取各種網(wǎng)站數(shù)據(jù)實戰(zhàn)案例

12306模擬登陸已改為滑動驗證,下面是個人手寫代碼,可借鑒下??
from time import sleep from selenium import webdriver from selenium.webdriver import ActionChains # 導入動作鏈對應(yīng)的類 from selenium.webdriver import ChromeOptions # 實現(xiàn)selenium反爬規(guī)避 if __name__ == "__main__": # 實現(xiàn)規(guī)避(主要規(guī)避代碼) option = ChromeOptions() option.add_argument("--disable-blink-features=AutomationControlled") option.add_experimental_option('excludeSwitches', ['enable-automation']) bro = webdriver.Chrome('./chromedriver.exe',options=option) bro.get('https://kyfw.12306.cn/otn/resources/login.html') # 選中密碼登錄并點擊 account = bro.find_element('id','J-userName') account.send_keys('Elmous') sleep(2) # passwd = bro.find_element('id','J-password') passwd.send_keys('xykzxh1314') sleep(2) # 選中登陸按鈕并點擊 btn = bro.find_element('id','J-login') btn.click() sleep(3) while True: try: # 動作鏈 span = bro.find_element('xpath','//*[@id="nc_1_n1z"]') actions = ActionChains(bro) # 行為鏈實例化 sleep(2) # 等待2秒鐘 # 經(jīng)截圖測量,滑塊需要滑過的距離為300像素 actions.click_and_hold(span).move_by_offset(300, 0).perform() # 滑動 actions.release(); sleep(2); a = bro.find_element('id',"nc_1_refresh1"); # 查找刷新按鈕,如果沒有說明登錄成功,執(zhí)行except跳出循環(huán) a.click(); # 如果剛剛滑動失敗,則點擊刷新,重新滑動 except Exception as e: print(e); break; sleep(2) bro.quit()
標簽: