Home > Software design >  How can i extract cookies like this?
How can i extract cookies like this?

Time:09-04

Hello guys I am making a bot in python requests to register a site and verify phone number however when I want to extract cookies I only get it like this this is one of the cookies: {'__cf_bm': 'Y0T3GzPQqXsWAnmYUSNWoaEogoto8_OELyA_wo_FUqQ-1662189807-0-Adw17pUjv2YaGcp3ynR1CX3CvGY/4AMBCJWqCn0GIk2qbGXQPifeKXlo5IN iaDCsqeXrTN32gNXpdybETEYT6U='}

I want to extract them like this for each cookie:

{
        "name": "__cf_bm",
        "value": "je.AFZpQXtMPL7oZPdgCV6Bo4UxCb3BqMYdu2L7Eol0-1662189210-0-ARq9RgTbX7ItgRQ6dBXYjEdvQS8/fP7JiVibVrHYspk37sUiN3mQOlnHBfkJdMG033inhnAIS92vthv8bLdeYYQ=",
        "domain": ".yemeksepeti.com",
        "hostOnly": false,
        "path": "/",
        "secure": true,
        "httpOnly": true,
        "sameSite": "no_restriction",
        "session": false,
        "firstPartyDomain": "",
        "partitionKey": null,
        "expirationDate": 1662191010,
        "storeId": "firefox-default",
        "id": 1
    }

this is the reproducible for register and then get the cookies from requests:

import json
import time
import requests
import selenium.webdriver
import urllib3
import random
import string
import secrets

password_length = 13
password = secrets.token_urlsafe(password_length)
file1 = open("Original.txt", "w")

kacAdet = int(input('Kaç adet hesap kaydetmek istiyorsunuz?'))

proxies = {
    'http': '23.229.62.241:3128',

}


def random_char(char_num):
    return ''.join(random.choice(string.ascii_letters) for _ in range(char_num))


email = random_char(4)
print(random_char(4))

for i in range(kacAdet):
    cookies = {
        'dhhPerseusGuestId': '1661866956.4391864905.SzayaOAaKf',
        '_gcl_au': '1.1.2045638547.1656310028',
        '_pxvid': '60553c6b-f5df-11ec-b676-644a4b4e486e',
        '_ga_W5PM07D07L': 'GS1.1.1661866956.12.1.1661871822.0.0.0',
        '_ga': 'GA1.2.302729117.1656310029',
        '_tq_id.TV-276372-1.c7a5': 'b3e61a11d2f82260.1656310030.0.1661871797..',
        '__ssid': '288a4fe9f40bd5024bd37c43ed7b18f',
        '_fbp': 'fb.1.1656310031494.1660145579',
        'device_token': 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleW1ha2VyLXZvbG8tZGV2aWNlLXlzLXRyIiwidHlwIjoiSldUIn0.eyJpZCI6Ijk2MzI1ZThhLTVjYmQtNDk3NC1iZmYzLTgzYjdjMWZhMDdiZSIsImNsaWVudF9pZCI6InZvbG8iLCJ1c2VyX2lkIjoidHI4anBkOHMiLCJleHBpcmVzIjo0ODA5OTEwMDQ0LCJ0b2tlbl90eXBlIjoiYmVhcmVyIiwic2NvcGUiOiJERVZJQ0VfVE9LRU4ifQ.qlV25H4fL5mN9uHVHK-0VGpC6UZziOMfxpY5EbDXRCPeitQY-dfks5CA60Qj59alrbna3Ew5SS96HZ7elMOCDWDk9Al3RSBDgCoZNhn10jVexrmqqVBebRZyF27qh49tb1WoGE5FzWvX0MvlZA-1fvXCciPsHlRuKzr6oKPr4iZ2ilRBye9YwClEdpnu2ymg19moJhZn8oDmVotNAnEOC4tsRC64nXHT14rnU3zNx6yds1eeIjImOM-1u99lxOjmbTuBkQJWNEUxucimnA2RUe3VmZKhFronpM46ChdZr0f8xU7hvxGCZ2NxSVlN-uN79oHYThIgcxbaP0NjPajRcg',
        'ab.storage.userId.f767a324-7168-4b7e-8687-652a3ac864bd': '{"g":"TR_44270161","c":1661869740576,"l":1661869740576}',
        'ab.storage.deviceId.f767a324-7168-4b7e-8687-652a3ac864bd': '{"g":"4c13c6a1-fbd2-04ec-5d84-b972ec0ba576","c":1656310045145,"l":1656310045145}',
        '_hjSessionUser_1543571': 'eyJpZCI6ImE2MGI0MTQ2LTAzMGQtNWUwOS05YjhmLWZmMGRjNGE4YjRlOCIsImNyZWF0ZWQiOjE2NTYzMTA3MDg3NzIsImV4aXN0aW5nIjp0cnVlfQ==',
        'AppVersion': '0ff5617',
        '_gid': 'GA1.2.267011508.1661808541',
        '__cf_bm': 'tjx3_.dOiiolQhAxWstqrkSP1CKvO_S1N4p.0EeINK4-1661871791-0-AUZhgLbPnAeoeXTbqYZ9zWdoMEV/paQVlVDqhYwfMhjTEpAzR9DL6w/R9PJ/UunzV1kS0E5eHo3I23DtfqQbu2g=',
        'dhhPerseusSessionId': '1661866956.9452682616.Q3sF0bSpb0',
        'dhhPerseusHitId': '1661871837551.356435850558823550.x7vphunw0q',
        'ab.storage.sessionId.f767a324-7168-4b7e-8687-652a3ac864bd': '{"g":"6aa2e303-cb5c-e1cd-d63c-ae74f0ef9a28","e":1661873591540,"c":1661869740577,"l":1661871791540}',
        'hl': 'tr',
        '_pxhd': '7s9aESLpFRxZZKv6tpZw1ARL/WCcfDvZ/5vEGc7NJzvNODk0RH90lYQB4Eua5Kqza/RouLT9ZEDCwApCgb6LWA==:fdJxKPXZt-rd6WPHmuK4PmpAXFudob0AhmJ-XL04XjMoHApgWh/7FNngAW72wqaDqlEmfZDx6D2EV-3kuOsLqWb15qT-85pfWFSmnayR70k=',
        '_px3': 'e298931c318130994efbdc870fa364efa1eab482a8e5234e8667c760ad79fba0:6SPR0 3825cPBG3QzlECszOACYSdPXJLRT/ifAegNKTC7Ky1is4UYFhA01ROdi9t5FPC67v5CjaqHtWa7tzTuQ==:1000:DVJEiHg7S1XrIagRXrM6VXHwU2ozpb8RE2lXnVttxjQFilQ GEHfG07sM 9BqFSgi69SNwFOgo8/C3CALs2K5rm1G7jyBJ432cNubNkJx jEy2YeDw5fFuUlytWWtZDt qcrwX9wwFJi/j6POWqnm9j1aZuVEztpuZnSLs0D0QZBZV0qaOPaQwxJw9vLCsigsgKcWGAbfbRMvVKXx0Cevw==',
        'pxcts': 'deb1b68a-2874-11ed-9c26-6c6e70485242',
        'tooltip-reorder': 'true',
        '_dc_gtm_UA-86673819-11': '1',
        '_dd_s': 'logs=1&id=38095de6-1b84-4b17-a32c-c539cfaf0dca&created=1661871796050&expire=1661872741919',
        '_pxff_fp': '1',
    }

    headers = {
        'Host': 'www.yemeksepeti.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0',
        'Accept': 'application/json',
        'Accept-Language': 'tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3',
        'Content-Type': 'multipart/form-data; boundary=---------------------------19743795514118722864129047497',
        'X-Requested-With': 'XMLHttpRequest',
        'X-Device-Fingerprint': 'db5f21349eff189ec4f7664477e01cd4',
        'X-Device': 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleW1ha2VyLXZvbG8tZGV2aWNlLXlzLXRyIiwidHlwIjoiSldUIn0.eyJpZCI6Ijk2MzI1ZThhLTVjYmQtNDk3NC1iZmYzLTgzYjdjMWZhMDdiZSIsImNsaWVudF9pZCI6InZvbG8iLCJ1c2VyX2lkIjoidHI4anBkOHMiLCJleHBpcmVzIjo0ODA5OTEwMDQ0LCJ0b2tlbl90eXBlIjoiYmVhcmVyIiwic2NvcGUiOiJERVZJQ0VfVE9LRU4ifQ.qlV25H4fL5mN9uHVHK-0VGpC6UZziOMfxpY5EbDXRCPeitQY-dfks5CA60Qj59alrbna3Ew5SS96HZ7elMOCDWDk9Al3RSBDgCoZNhn10jVexrmqqVBebRZyF27qh49tb1WoGE5FzWvX0MvlZA-1fvXCciPsHlRuKzr6oKPr4iZ2ilRBye9YwClEdpnu2ymg19moJhZn8oDmVotNAnEOC4tsRC64nXHT14rnU3zNx6yds1eeIjImOM-1u99lxOjmbTuBkQJWNEUxucimnA2RUe3VmZKhFronpM46ChdZr0f8xU7hvxGCZ2NxSVlN-uN79oHYThIgcxbaP0NjPajRcg',
        'Origin': 'https://www.yemeksepeti.com',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'Referer': 'https://www.yemeksepeti.com/login/new?step=registration',
    }

    data = '-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="_target_path"\r\n\r\nhttps://www.yemeksepeti.com/\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[first_name]"\r\n\r\nmert\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[last_name]"\r\n\r\ndemir\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[email]"\r\n\r\nmertdemir'   email   str(i)   '@gmail.com\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[password]"\r\n\r\n' str(password) '\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[birthdate]"\r\n\r\n1999-10-01\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[terms_and_conditions_consent]"\r\n\r\nagreed\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[marketing_consent]"\r\n\r\nopt-in\r\n-----------------------------19743795514118722864129047497\r\nContent-Disposition: form-data; name="customer[marketing_sms_consent]"\r\n\r\nopt-in\r\n-----------------------------19743795514118722864129047497--\r\n'

    response = requests.post('https://www.yemeksepeti.com/api/v1/customers/async_register', cookies=cookies,
                             headers=headers, data=data, verify=False)
    my_cookies = requests.utils.dict_from_cookiejar(response.cookies)
    print(my_cookies)
    print(response.status_code)

CodePudding user response:

Don't use dict_from_cookiejar but write own code to convert it.

for item in response.cookies:
    print('name  :', item.name)
    print('value :', item.value)
    print('domain:', item.domain)
    print('path  :', item.path)
    # ... other attributes ...
    print('---')

EDIT:

Eventually you can use __dict__ to get all attributes as dictionary.

for item in response.cookies:
    print(item.__dict__)

You may also get original data as string and parse it on your own.

print( response.headers['Set-Cookie'] )

EDIT:

import json

all_items = []

for item in response.cookies:
    all_items.append(tem.__dict__)

# --- write ---

text = json.dumps(all_items)

with open('data.json', 'w') as fh:
    fh.write(text)
    
# --- read ---

with open('data.json') as fh:
    text = fh.read()

all_items = json.loads(text)
  • Related