def json_to_cookie(self, json_cookie_list): cookies_list_info = [] for json_cookie in json_cookie_list: c = QNetworkCookie() print(json_cookie, '** json_cookie') for k, v in json_cookie.items(): if k == 'path': c.setPath(v) elif k == 'domain': c.setPath(v) elif k == 'expirationDate': print(v, type(v)) qdate = QDateTime(2019, 12, 20, 11, 59, 59) print(qdate) c.setExpirationDate(qdate) elif k == 'httponly': c.setHttpOnly(v) elif k == 'secure': c.setSecure(v) elif k == 'value': c.setValue(v.encode()) elif k == 'name': c.setName(v.encode()) print('c', c.expirationDate()) cookies_list_info.append(c) return cookies_list_info
def get_qnetwork_cookie(self): cookie = QNetworkCookie() cookie.setExpirationDate(self.expiration_date) cookie.setHttpOnly(self.http_only) cookie.setValue(self.value) cookie.setSecure(self.secure) cookie.setPath(self.path) cookie.setDomain(self.domain) cookie.setName(self.name) return cookie
def cookie(self): cookie = QNetworkCookie() cookie.setDomain(self.m_domainLineEdit.text()) cookie.setName(self.m_nameLineEdit.text().encode()) cookie.setValue(self.m_valueLineEdit.text().encode()) cookie.setExpirationDate(QDateTime(self.m_dateEdit.date())) cookie.setPath(self.m_pathLineEdit.text()) cookie.setSecure( self.m_isSecureComboBox.currentText() == self.tr("yes")) cookie.setHttpOnly( self.m_isHttpOnlyComboBox.currentText() == self.tr("yes")) return cookie
def initCookies(self): for cookie in cookies: qcookie = QNetworkCookie() qcookie.setName(cookie.get('name', '').encode()) qcookie.setValue(cookie.get('value', '').encode()) qcookie.setDomain(cookie.get('domain', '')) qcookie.setPath(cookie.get('path', '')) qcookie.setExpirationDate( QDateTime.fromString(str(cookie.get('expirationDate', 0)), Qt.ISODate)) qcookie.setHttpOnly(cookie.get('httpOnly', False)) qcookie.setSecure(cookie.get('secure', False)) # 注意可以设置具体的url self.cookieStore.setCookie(qcookie, QUrl())
def add(self, cookie_dict, scheme, hostname, path="/"): cookie_list = [] for key, value in cookie_dict.items(): cookie = QNetworkCookie() cookie.setDomain(hostname) cookie.setPath(path) key_b = QByteArray() key_b.append(key) cookie.setName(key_b) value_b = QByteArray() value_b.append(value) cookie.setValue(value_b) cookie_list.append(cookie) self._cookie_jar.setCookiesFromUrl(cookie_list, QUrl(str(scheme)))
def har_cookie2qt(cls, cookie): qcookie = QNetworkCookie() qcookie.setName(to_bytes(cookie["name"])) qcookie.setValue(to_bytes(cookie["value"])) if 'domain' in cookie: qcookie.setDomain(cookie["domain"]) if 'httpOnly' in cookie: qcookie.setHttpOnly(cookie["httpOnly"]) if 'secure' in cookie: qcookie.setSecure(cookie["secure"]) if 'path' in cookie: qcookie.setPath(cookie["path"]) if cookie.get('expires'): expires = QDateTime.fromString(cookie["expires"], Qt.ISODate) qcookie.setExpirationDate(expires) return qcookie
def har_cookie2qt(cls, cookie): qcookie = QNetworkCookie() qcookie.setName(to_bytes(cookie["name"])) qcookie.setValue(to_bytes(cookie["value"])) if 'domain' in cookie: qcookie.setDomain(cookie["domain"]) if 'httpOnly' in cookie: qcookie.setHttpOnly(cookie["httpOnly"]) if 'secure' in cookie: qcookie.setSecure(cookie["secure"]) if 'path' in cookie: qcookie.setPath(cookie["path"]) if cookie.get('expires'): expires = QDateTime.fromString(cookie["expires"], Qt.ISODate) qcookie.setExpirationDate(expires) return qcookie
def __init__(self): super().__init__() self.setWindowTitle('My Browser') self.resize(800, 600) url = 'https://www.baidu.com' req_cookies = { 'BD_UPN': '', 'PSTM': '', 'BIDUPSID': '', 'BAIDUID': '', 'BDORZ': '', 'delPer': '', 'BD_CK_SAM': '', 'PSINO': '', 'BDRCVFR[feWj1Vr5u3D]': '', 'ZD_ENTRY': '', 'H_PS_645EC': '', 'BDUSS': '', 'BD_HOME': '', 'H_PS_PSSID': '' } self.webview = QWebEngineView() cookiestore = self.webview.page().profile().cookieStore() cookie = QNetworkCookie() for key, value in req_cookies.items(): # 设置cookie的键 cookie.setName(key.encode()) # 设置cookie的值 cookie.setValue(value.encode()) # 设置cookie的域 cookie.setDomain('.baidu.com') cookiestore.setCookie(cookie) self.webview.load(QUrl(url)) self.setCentralWidget(self.webview)