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

P57 12306模擬登陸(滑動驗證)
""" @Author:lalala @File:07.12306模擬登陸.py @Date:2023/03/10 """ import time from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver import ChromeOptions # 配置瀏覽器的設置 option = ChromeOptions() # 規(guī)避selenium被檢測到的風險 option.add_experimental_option('excludeSwitches',['enable-automation']) option.add_argument('--disable-blink-features=AutomationControlled') # 定義chrome驅動地址 path = Service('chromedriver.exe') # 創(chuàng)建瀏覽器操作對象 browser = webdriver.Chrome(service=path,options=option) # 設置隱式等待時間,5s內找到了元素就繼續(xù)執(zhí)行,找不到報超時(為了防止找元素的時候元素還沒加載出來) browser.implicitly_wait(5) # 訪問網(wǎng)站 browser.get('https://kyfw.12306.cn/otn/resources/login.html') # 定位輸入賬號密碼,點擊登陸 browser.find_element(By.ID, "J-userName").send_keys("xxxx") browser.find_element(By.ID, "J-password").send_keys("xxxx") browser.find_element("id", "J-login").click() # 獲取滑塊和滑塊條 # slider = browser.find_element("class name","nc_iconfont") slider = browser.find_element(By.XPATH,'//*[@id="nc_1_n1z"]') slider_length = browser.find_element("class name","nc-lang-cnt") # print(slider_length.size) # 創(chuàng)建動作鏈,長按點擊滑塊,拖動滑塊 action = ActionChains(browser) action.click_and_hold(slider) action.move_by_offset(slider_length.size['width'], 0).perform() action.release() time.sleep(5) browser.quit()
標簽: