Pumba
Pumba

Reputation: 181

How to convert requests.cookiejar to qnetworkcookiejar?

Is there a simple way to convert a cookiejar from the Python 3 requests library to a qnetworkcookiejar?

I convert the cookiejar from the requests library into a dictionary and then in a qnetworkcookiejar. Some cookies are there in multiple versions with different values.

def updateCookieJar(self, cookiejar, requested_url):     
    qnetworkcookie_list = []
    cookie_dict = dict_from_cookiejar(cookiejar)
    for cookie in cookie_dict: 
        tmp_cookiejar = QNetworkCookie(cookie, cookie_dict[cookie])
        qnetworkcookie_list.append(tmp_cookiejar)
    qcookiejar = QNetworkCookieJar()
    qcookiejar.setCookiesFromUrl(qnetworkcookie_list, QUrl(requested_url))
    self.networkAccessManager().setCookieJar(qcookiejar)

This function is called inside a Webpage.

Upvotes: 15

Views: 974

Answers (1)

Kashyap Prajapati
Kashyap Prajapati

Reputation: 442

Try using cookiejar directly instead of dictionary.

def updateCookieJar(self, cookiejar, requested_url):     
    qnetworkcookie_list = []

    for cookie in cookiejar:
        tmp_cookiejar = QNetworkCookie(cookie.name, cookie.value)
        qnetworkcookie_list.append(tmp_cookiejar)
    qcookiejar = QNetworkCookieJar()
    qcookiejar.setCookiesFromUrl(qnetworkcookie_list, QUrl(requested_url))
    self.networkAccessManager().setCookieJar(qcookiejar)

Upvotes: 1

Related Questions