1. 程式人生 > >selenium+python如何利用cookie免密碼登入

selenium+python如何利用cookie免密碼登入

1. 首先使用使用者名稱和賬號,登入獲取cookie

import json
import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException


class Crawler():
    def gather():
        chrome_options = Options()
        chrome_options.add_argument("window-size=1024,768")
        driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\devtool\Anaconda\Scripts\chromedriver')
        wait = WebDriverWait(driver, 1)
        ##登入百度知道
        logurl = 'https://zhidao.baidu.com/'
        #登入前清楚所有cookie
        driver.delete_all_cookies()
        driver.get(logurl)
        ##登入前列印cookie
        print(driver.get_cookies())

        ##點選登入按鈕
        driver.find_element_by_xpath('//*[@id="userbar-login"]').click()
        # driver.find_element_by_id("userbar-login").click()
        time.sleep(2)
        ##首次嘗試的 預設進入掃碼登入的介面
        try:
            footerULoginBtn = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]')
            footerULoginBtn.click()  #切換到使用者名稱和密碼登入
            footerULoginBtn_not_exist = False
        except:
            footerULoginBtn_not_exist = True

        ## 使用者名稱跟密碼的設定並點選提交
        user = driver.find_element_by_name('userName')
        user.clear()
        pwd = driver.find_element_by_name('password')
        pwd.clear()
        submit = driver.find_element_by_id('TANGRAM__PSP_10__submit')
        time.sleep(2)
        user.send_keys('使用者名稱')
        pwd.send_keys('密碼')
        time.sleep(1)
        submit.click()
        time.sleep(1)
        ## 傳送手機驗證碼 驗證
        ##點擊發送按鈕
        ###是否需要輸入手機驗證碼
        try:
            driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_send_mobile"]').click()
            time.sleep(10)
            ##使用shell互動式,接受驗證碼
            message = input("Tell me the captcha: ")
            ##輸入驗證碼
            captcha = driver.find_element_by_xpath('//*[@id="TANGRAM__28__input_label_vcode"]')
            time.sleep(1)
            captcha.send_keys(message)
            time.sleep(1)
            ##點選提交
            driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_submit"]').click()
            time.sleep(3)
        except:
            time.sleep(1)

        ### 獲取cookie
        cookie = driver.get_cookies()
        print(cookie)
        jsonCookies = json.dumps(cookie)
        with open('vcyber.json', 'w') as f:
            f.write(jsonCookies)

        time.sleep(30)

Crawler.gather()

獲取cookie後,可以不用輸入密碼登入

import json
import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException


class Crawler():
    def gather():
        chrome_options = Options()

        chrome_options.add_argument("window-size=1024,768")
        driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\devtool\Anaconda\Scripts\chromedriver')
        wait = WebDriverWait(driver, 1)

        ##登入百度知道
        logurl = 'https://zhidao.baidu.com/'

        #登入前清楚所有cookie
        driver.delete_all_cookies()
        driver.get(logurl)

        f1 = open('vcyber.json')
        cookie = f1.read()
        cookie = json.loads(cookie)
        for c in cookie:
            driver.add_cookie(c)
        # # 重新整理頁面
        driver.refresh()

Crawler.gather()