Home > Enterprise >  how can I transform a 'cookie' in a string keeping all the info?
how can I transform a 'cookie' in a string keeping all the info?

Time:05-24

I want to transform some cookies in strings, in order to reuse then. The problem is that the cookies are full of quotes and symbols, and when I try to turn it into a string I lose a lot of the information

This is what a have:

response.cookies

<RequestsCookieJar[Cookie(version=0, name='B2W-IU', value='false', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='MobileOptOut', value='1', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='b2wChannel', value='INTERNET', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='b2wDevice', value='eyJvcyI6IldpbmRvd3MgTlQiLCJvc1ZlcnNpb24iOiI2LjIiLCJ2ZW5kb3IiOiJDaHJvbWUiLCJ0eXBlIjoiZGVza3RvcCIsIm1rdE5hbWUiOiJDaHJvbWUgMjciLCJtb2RlbCI6IjI3IiwibW9iaWxlT3B0T3V0IjoiZmFsc2UifQ==', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='b2wDeviceType', value='desktop', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='b2wEPar', value='bo_nh_bs_go_busca', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='b2wOpn', value='GORGSUBA', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='catalogTestAB', value='out', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1653156228, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='searchTestAB', value='old', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1653156228, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='subaEPar', value='bo_nh_bs_go_busca', port=None, port_specified=False, domain='.submarino.com.br', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='_pxhd', value='8QFznBMvV/UViSdPl1C5IDbeQiRGthyHlI5oYsCVK71TQdFhpe4Uw90dDUJpQP4duHrnW080Pu7rdKZT1wGHUw==:BDX87p21U4qYYNF2lXm5DszA5zV3kankyeHrROSDU9MluFoI0j2g6V/CO7c3manz4PdqmKtv2xoNvzTuppOhpIRzutgdjH1nMBAVevOnAAc=', port=None, port_specified=False, domain='www.submarino.com.br', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=1684605828, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>

And when I try to transform it in a string, this is what a get:

str(response.cookies)

'<RequestsCookieJar[<Cookie B2W-IU=false for .submarino.com.br/>, <Cookie MobileOptOut=1 for .submarino.com.br/>, <Cookie b2wChannel=INTERNET for .submarino.com.br/>, <Cookie b2wDevice=eyJvcyI6IldpbmRvd3MgTlQiLCJvc1ZlcnNpb24iOiI2LjIiLCJ2ZW5kb3IiOiJDaHJvbWUiLCJ0eXBlIjoiZGVza3RvcCIsIm1rdE5hbWUiOiJDaHJvbWUgMjciLCJtb2RlbCI6IjI3IiwibW9iaWxlT3B0T3V0IjoiZmFsc2UifQ== for .submarino.com.br/>, <Cookie b2wDeviceType=desktop for .submarino.com.br/>, <Cookie b2wEPar=bo_nh_bs_go_busca for .submarino.com.br/>, <Cookie b2wOpn=GORGSUBA for .submarino.com.br/>, <Cookie catalogTestAB=out for .submarino.com.br/>, <Cookie searchTestAB=old for .submarino.com.br/>, <Cookie subaEPar=bo_nh_bs_go_busca for .submarino.com.br/>, <Cookie _pxhd=8QFznBMvV/UViSdPl1C5IDbeQiRGthyHlI5oYsCVK71TQdFhpe4Uw90dDUJpQP4duHrnW080Pu7rdKZT1wGHUw==:BDX87p21U4qYYNF2lXm5DszA5zV3kankyeHrROSDU9MluFoI0j2g6V/CO7c3manz4PdqmKtv2xoNvzTuppOhpIRzutgdjH1nMBAVevOnAAc= for www.submarino.com.br/>]>'

So, as you can see, Im losing a lot of information, how can I transform the cookie in a string keeping all the info?

thanks in advance

CodePudding user response:

Regarding requests.cookies.RequestsCookieJar there is a get_dict method

d = response.cookies.get_dict()

Source code

def get_dict(self, domain=None, path=None):
    """Takes as an argument an optional domain and path and returns a plain
    old Python dict of name-value pairs of cookies that meet the
    requirements.

    :rtype: dict
    """
    dictionary = {}
    for cookie in iter(self):
        if (domain is None or cookie.domain == domain) and (
            path is None or cookie.path == path
        ):
            dictionary[cookie.name] = cookie.value
    return dictionary
  • Related