詳解Selenium如何實(shí)現(xiàn)獲取cookies并保存
一、獲取cookie
獲取到cookie后,保存到文件中
from selenium import webdriver import time import json from selenium.webdriver.common.by import By #創(chuàng)建webdriver 對(duì)象,指明使用chrome 瀏覽器驅(qū)動(dòng) wd = webdriver.Chrome() wd.implicitly_wait(10) #調(diào)用webdriver 對(duì)象的get方法,可以讓瀏覽器打開指定網(wǎng)址 wd.get('https://zhidao.baidu.com/activity/iknowduck/level?actId=47') input('網(wǎng)頁(yè)端登錄百度賬號(hào)后,請(qǐng)按回車鍵') cookie= wd.get_cookies() # #將獲得cookie 的信息打印 print(cookie) with open('baiducookies.txt','w') as f: # 將cookies保存為json格式 f.write(json.dumps(wd.get_cookies())) f.close()
二、加載cookie
通過(guò)讀取txt文件,添加到瀏覽器中
wd.add_cookie(cookie)
完整的讀取cookie的流程
from selenium import webdriver import time import json from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options from chatgpt_demo import chatgpt #創(chuàng)建webdriver 對(duì)象,指明使用chrome 瀏覽器驅(qū)動(dòng) wd= webdriver.Chrome() wd.implicitly_wait(10) #調(diào)用webdriver 對(duì)象的get方法,可以讓瀏覽器打開指定網(wǎng)址 wd.get('https://zhidao.baidu.com/activity/iknowduck/level?actId=47') # 首先清除由于瀏覽器打開已有的cookies wd.delete_all_cookies() time.sleep(10) #打開cookie文本,使用已保存的cookie登錄 with open('baiducookies.txt','r') as f: # 使用json讀取cookies 注意讀取的是文件 所以用load而不是loads cookies_list = json.load(f) for cookie in cookies_list: wd.add_cookie(cookie) wd.refresh() #刷新頁(yè)面 wd.refresh() #刷新頁(yè)面 time.sleep(6) #獲取當(dāng)前窗口的title First_handle = wd.current_window_handle j = 0 while j < 60: #try: for i in range(9): n = 1 #點(diǎn)擊第一個(gè)問(wèn)題 questions = wd.find_elements(By.CSS_SELECTOR, 'div.q-item > div.q-title > span:nth-child(2) ') for question in questions: print('第{}個(gè)問(wèn)題:'.format(n), question.text) n += 1 #將第一個(gè)問(wèn)題,輸入catgpt在線智能回答 message = chatgpt(question.text) print(message) #with open("./output/{}.doc".format(prompt), "w") as of: # of.write(message) titles = question.text + '_百度知道' time.sleep(1) #點(diǎn)擊“回答”,按鈕 # answer = wd.find_element(By.CSS_SELECTOR,'.goto-anwser-btn') # print('點(diǎn)擊:',answer.text) wd.add_cookie(cookie) #帶cookie question.click() #點(diǎn)擊問(wèn)題 time.sleep(3) #切換窗口,點(diǎn)擊“去回答”按鈕時(shí),打開了新的窗口,但WebDriver對(duì)象對(duì)應(yīng)的 還是老窗口。這里要跟隨跳轉(zhuǎn) for handle in wd.window_handles: # 先切換到該窗口 wd.switch_to.window(handle) # 得到該窗口的標(biāo)題欄字符串,判斷是不是我們要操作的那個(gè)窗口 if 'titles' in wd.title: # 如果是,那么這時(shí)候WebDriver對(duì)象就是對(duì)應(yīng)的該該窗口,正好,跳出循環(huán), break #print(wd.title) #新窗口下,不管是否已有其他回答,都點(diǎn)擊“我來(lái)答按鈕” element = wd.find_element(By.CSS_SELECTOR, '#answer-bar') element.click()#點(diǎn)擊“我來(lái)答按鈕” #輸入文本 #新窗口,切換到第一級(jí)iframe框下,正文內(nèi)容 wd.switch_to.frame('ueditor_0') element = wd.find_element(By.CSS_SELECTOR,'body > p') print('正在輸入答案') element.send_keys(message) #先返回到主html,點(diǎn)擊提交按鈕 wd.switch_to.default_content() time.sleep(50) element = wd.find_element(By.CSS_SELECTOR,'div.addons.line > a') print(element.text) wd.add_cookie(cookie) # 帶cookie element.click() time.sleep(5) #做完一系列操作后關(guān)閉school_handle wd.close() # 切換窗口會(huì)第一個(gè)窗口 wd.switch_to.window(First_handle) #except: print("出錯(cuò){}次,正在重新運(yùn)行程序。".format(j)) j += 1 wd.switch_to.window(First_handle) time.sleep(5) wd.refresh() # 刷新頁(yè)面 else: print('出錯(cuò)太多次啦,程序已結(jié)束')
到此這篇關(guān)于詳解Selenium如何實(shí)現(xiàn)獲取cookies并保存的文章就介紹到這了,更多相關(guān)Selenium獲取cookies內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python利用urllib和urllib2訪問(wèn)http的GET/POST詳解
urllib模塊提供的上層接口,使我們可以像讀取本地文件一樣讀取www和ftp上的數(shù)據(jù)。下面這篇文章主要給大家介紹了關(guān)于python如何利用urllib和urllib2訪問(wèn)http的GET/POST的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09python+mysql實(shí)現(xiàn)個(gè)人論文管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python+mysql實(shí)現(xiàn)個(gè)人論文管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)例分析
這篇文章主要介紹了python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)現(xiàn)方法,實(shí)例分析了Python基于UDP協(xié)議的數(shù)據(jù)傳輸實(shí)現(xiàn)方法,需要的朋友可以參考下2015-05-05Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié)
這篇文章主要介紹了Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié),本文總結(jié)了最基本最常用的一些操作,需要的朋友可以參考下2014-11-11Pandas數(shù)據(jù)清洗函數(shù)總結(jié)
本文主要介紹了Pandas數(shù)據(jù)清洗函數(shù)總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01詳解python的網(wǎng)絡(luò)編程基礎(chǔ)
這篇文章主要為大家介紹了python網(wǎng)絡(luò)編程的基礎(chǔ),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01Python把圖片轉(zhuǎn)化為pdf代碼實(shí)例
這篇文章主要介紹了Python把圖片轉(zhuǎn)化為pdf代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07