1. 程式人生 > 其它 >python爬蟲-selenium模擬登入

python爬蟲-selenium模擬登入

模擬登入qq空間:有iframe、無驗證碼

"""
selenium模擬登入QQ空間:有iframe、無驗證碼
"""
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

if __name__ == '__main__':
    u = '你的賬戶'
    p = '你的密碼'
    # 例項化谷歌瀏覽器物件
    service = Service('./chromedriver')
    bro = webdriver.Chrome(service=service)
    # 請求頁面
    url = 'https://qzone.qq.com'
    bro.get(url=url)

    # 定位到登入的iframe
    bro.switch_to.frame('login_frame')

    # 使用動作鏈:from selenium.webdriver import ActionChains
    # 例項化動作鏈物件
    action = ActionChains(bro)

    # 找到並點選賬號密碼登入
    change_plogin = bro.find_element(by=By.XPATH, value='//a[@id="switcher_plogin"]')
    action.click(change_plogin).perform()

    # 找到賬號框
    user_input = bro.find_element(by=By.XPATH, value='//input[@id="u"]')
    user_input.send_keys(u)
    # 找到密碼框
    pwd_input = bro.find_element(by=By.XPATH, value='//input[@id="p"]')
    pwd_input.send_keys(p)
    # 找到登入按鈕
    login_button = bro.find_element(by=By.XPATH, value='//input[@id="login_button"]')
    # 點選登入按鈕
    action.click(login_button).perform()
    # 釋放動作鏈
    action.release().perform()

    # 關閉瀏覽器物件
    # bro.quit()



模擬登入12306:無iframe、有滑動驗證碼、有特徵識別

"""
模擬登入12306:無iframe,滑動驗證碼,檢測selenium
"""
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ChromeOptions  # 實現規避檢測風險

if __name__ == '__main__':
    # 你的使用者名稱密碼
    username = '你的賬戶'
    password = '你的密碼'

    service = Service('./chromedriver')
    chrome_options = ChromeOptions()
    # 規避檢測
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
    # 例項化瀏覽器
    bro = webdriver.Chrome(service=service,options=chrome_options)

    # 指定url
    url = 'https://kyfw.12306.cn/otn/resources/login.html'
    bro.get(url=url)

    # 找到使用者框
    user_input = bro.find_element(by=By.XPATH, value='//input[@id="J-userName"]')
    # 傳入你的使用者名稱
    user_input.send_keys(username)

    # 找到密碼框
    pwd_input = bro.find_element(by=By.XPATH, value='//input[@id="J-password"]')
    # 傳入你的密碼
    pwd_input.send_keys(password)

    # 找到登入按鈕/連結
    a_login = bro.find_element(by=By.XPATH, value='//a[@id="J-login"]')
    # 點選 a_login.click()
    a_login.click()

    # 使用動作鏈:from selenium.webdriver import ActionChains
    # 例項化動作鏈物件
    action = ActionChains(bro)

    # 等待兩秒,不然會找不到滑塊
    time.sleep(2)

    # 解決特徵識別
    script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
    bro.execute_script(script)

    # 這時候會出現一個滑動驗證碼
    slid_span = bro.find_element(by=By.ID, value='nc_1_n1z')
    # 按住並滑動340px
    action.click_and_hold(slid_span).perform()
    for i in range(10):
        action.move_by_offset(34, 0).perform()
        time.sleep(0.2)
    action.release().perform()
    # bro.quit()