Selenium保存和设置网页cookies
作者:YXN-python 阅读量:112 发布日期:2022-10-01
保存
import pickle # 用来保存对象或者是变量到文件中
from selenium import webdriver
# 进入网页
url = 'https://www.bilibili.com/'
broswer = webdriver.Chrome()
broswer.get(url=url)
input("请登录后按Enter键继续")
# 得到网页的cookies
broswer.refresh()
cookies = broswer.get_cookies()
# 写入cookie
pickle.dump(cookies, open('cookies.pkl', 'wb'))
设置
# 读取cookie
import pickle
from selenium import webdriver
# 读取保存的cookies
def readCookies():
return pickle.load(open("cookies.pkl", "rb"))
broswer = webdriver.Chrome()
url = 'https://www.bilibili.com/'
broswer.get(url)
# 删除原来的cookie
broswer.delete_all_cookies()
# 设置新的cookies
cookies = readCookies()
for cookie in cookies:
broswer.add_cookie(cookie)
# 刷新网页
broswer.refresh()
input('yes?')
YXN-python
2022-10-01