from selenium import webdriver import time driver = webdriver.Firefox() driver.get(url) driver.delete_all_cookies() #删除所有cookies
driver.add_cookie({'name':'JSESSIONID','value':'C37536D4DEC82AF5D7D3A27EA58A0397'}) #添加cookies
driver.get(url)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
保存cookies,读取cookies
#保存cookies到文件
import requests, pickle
session = requests.session()
# Make some calls
with open('somefile', 'wb') as f:
pickle.dump(session.cookies, f)
#从文件读取cookies
session = requests.session() # or an existing session
with open('somefile', 'rb') as f:
session.cookies.update(pickle.load(f))
-----------------------------------------------------------
def save_cookie():
with open('../cookies','w') as f:
pickle.dump(utils.dict_from_cookiejar(r.cookies), f)
#将RequestsCookieJar类对象转换成了dict格式,然后写入文件。
def get_cookie():
with open('../cookies','rb') as f:
cookies = utils.cookiejar_from_dict(pickle.load(f))
#先是用load读出了内容,然后将dict内容转换成RequestsCookieJar类对象格式,除此之外,还把RequestsCookieJar类对象,赋给了cookies这个变量