Bootstrap

selenium 获取cookie 并使用

selenium 获取cookie

参数设置 以获取阿里云cookie范例

from selenium import webdriver
import json
url = "https://account.aliyun.com/login/login.htm"

打开浏览器

# 加载模块
from selenium import webdriver
driver_path = 'chromedriver.exe'
# 打开浏览器
brower = webdriver.Chrome(driver_path)
brower.get(url)

此时打开一个google浏览器 打开阿里云站
可以手动在 此网站 登录自己的 账号

获取cookie 并保存

dictCookies=browser.get_cookies()
jsonCookies=json.dumps(dictCookies)
with open('cookies.json','w') as f:
	f.write(jsonCookies)

使用cookie

重新打开 一个 selenium

from selenium import webdriver
import json

driver_path = 'chromedriver.exe'
# 打开浏览器
brower = webdriver.Chrome(driver_path)

url = "https://account.aliyun.com/login/login.htm"

with open('cookies.json','r', encoding='utf-8') as f:
	listCookies = json.loads(f.read())
for cookie in listCookies:
	if 'expiry' in cookie:
		del cookie['expiry']
	brower.add_cookie(cookie)
brower.get(url)

;