PyQt5(>= 5.4) 设置cookie

时间:2020-12-03 23:04:26

1. 得到 cooieStore (QWebEngineCookieStore)

browser = QWebEngineView()
cookieStore = browser.page().profile().cookieStore()

2. 创建并设置cookie (QNetworkCookie)

cookie=QNetworkCookie()
cookie.setName(name.encode())   # QByteArray
cookie.setValue(value.encode()) # QByteArray
cookie.setDomain(url)           # QString
cookie.setPath(url)             # QSring 

QString <=>python str
QByteArray <=> python str (bytes)
另外设置cookie 过期时间等 optional 选项
cookie 介绍
Domain : 后缀限制
Path: 前缀限制

3. cookieStore 加载 cookie

cookieStore.setCookie(cookie)