您好,登錄后才能下訂單哦!
進(jìn)過兩天的研究終于實(shí)現(xiàn)了cookie的免密登錄,其實(shí)就是session。特別開心,因?yàn)樵赑ython爬蟲群里問那些大佬,可是他們的回答令我寒心,自己琢磨!?。?/p>
靠誰比如靠自己,為此我總結(jié)下經(jīng)驗(yàn),以免入門的小白再次踩這樣的吭。其實(shí)網(wǎng)上很多博客寫的都比較不詳細(xì)甚
首先問題自己思考,不懂得去群里問問,然后最重要的要理解事物的本質(zhì),只有理解后才能運(yùn)用它;最后在百度一下把所有相關(guān)的博客都點(diǎn)開,一條一條的看一遍總結(jié)下規(guī)律,比較嘍的就舍棄,這樣基本上都能得到啟發(fā)作用。比如selenium的本質(zhì)就是操作瀏覽器的,那么操作cookie也是操作瀏覽器的,并且用selenium打開瀏覽器時(shí)什么都沒有,如果進(jìn)入某個(gè)網(wǎng)站就會(huì)生成cookie先關(guān)信息。等待,那么我們可以用selenium打開網(wǎng)站之后讓他清除所有cookie避免干擾,然后睡眠20秒以便等我們登陸,這是就會(huì)產(chǎn)生cookie,我們將它獲取下來就可以實(shí)現(xiàn)登陸了。
下面直接上代碼:
# 登錄 def login_jd(): # 登錄前清除所有cookie browser.get('https://www.baidu.com/') browser.delete_all_cookies() # 打印登錄前的cookie cookieBefore = browser.get_cookies() print(cookieBefore) print("------------------------------------------------------------------------") time.sleep(2) list_cookies = [ {'domain': '.baidu.com', 'httpOnly': False, 'name': 'H_PS_PSSID', 'path': '/', 'secure': False, 'value': '1420_21120_29074_29237_2518_2909_29134_2832_28585_26350_2913'}, {'domain': '.baidu.com', 'expiry': 159184274.325927, 'httpOnly': False, 'name': 'BAIDUID', 'path': '/', 'secure': False, 'value': 'FF789623EA1785FF0D55ED0401D057B2:FG=1'}, {'domain': 'www.baidu.com', 'expiry': 1563289, 'httpOnly': False, 'name': 'BD_UPN', 'path': '/', 'secure': False, 'value': '123143'}, {'domain': '.baidu.com', 'expiry': 1560354688.892916, 'httpOnly': False, 'name': 'BDORZ', 'path': '/', 'secure': False, 'value': 'B490B5BF6F3CD40255D22BCDA1598'}, {'domain': '.baidu.com', 'expiry': 1819468287.471656, 'httpOnly': True, 'name': 'BDUSS', 'path': '/', 'secure': False, 'value': 'tGem1JzblNpOa1VVek0yLThmbWR6RxENWZtcVNwUThpT2hCLTVsUFBQUFBJCQAAAAAAAAAAAEAAAA4MdBAd3d3d2dnZ2dnZWVlZWUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALkI~1y5CP9cVm'}, {'domain': '.baidu.com', 'expiry': 2569420288, 'httpOnly': False, 'name': 'BIDUPSID', 'path': '/', 'secure': False, 'value': '587A2695FCD6D043A5FE5139E4F'}, {'domain': '.baidu.com', 'expiry': 3707755.819949, 'httpOnly': False, 'name': 'PSTM', 'path': '/', 'secure': False, 'value': '156027786'}, {'domain': 'www.baidu.com', 'httpOnly': False, 'name': 'BD_HOME', 'path': '/', 'secure': False, 'value': '1'} ] # 獲取之后的cookie cookies = browser.get_cookies() print(browser.get_cookies()) # 將獲取的的所有cookies添加到瀏覽器 for cookie in list_cookies: browser.add_cookie(cookie) print(cookie) time.sleep(2) # 刷新頁面即可更新cookie browser.refresh() time.sleep(505) browser.quit()
示例二:
首先使用用戶名和賬號,登錄獲取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()) ##點(diǎn)擊登錄按鈕 driver.find_element_by_xpath('//*[@id="userbar-login"]').click() # driver.find_element_by_id("userbar-login").click() time.sleep(2) ##首次嘗試的 默認(rèn)進(jìn)入掃碼登錄的界面 try: footerULoginBtn = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]') footerULoginBtn.click() #切換到用戶名和密碼登錄 footerULoginBtn_not_exist = False except: footerULoginBtn_not_exist = True ## 用戶名跟密碼的設(shè)置并點(diǎn)擊提交 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) ## 發(fā)送手機(jī)驗(yàn)證碼 驗(yàn)證 ##點(diǎn)擊發(fā)送按鈕 ###是否需要輸入手機(jī)驗(yàn)證碼 try: driver.find_element_by_xpath('//*[@id="TANGRAM__28__button_send_mobile"]').click() time.sleep(10) ##使用shell交互式,接受驗(yàn)證碼 message = input("Tell me the captcha: ") ##輸入驗(yàn)證碼 captcha = driver.find_element_by_xpath('//*[@id="TANGRAM__28__input_label_vcode"]') time.sleep(1) captcha.send_keys(message) time.sleep(1) ##點(diǎn)擊提交 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()
到此這篇關(guān)于Python3+selenium實(shí)現(xiàn)cookie免密登錄的示例代碼的文章就介紹到這了,更多相關(guān)selenium cookie免密登錄內(nèi)容請搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。