简体   繁体   中英

Python read Cookies from response?

In python I have:

cookies = dict(PHPSESSID='PHPSESSID=djsgkjhsdjkhj34',
               authchallenge='sdifhshdfiuh34234234',
               rishum='skdhfuihuisdhf-' + '10403111')

try:
    response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

But I'm looking to use cookies value for the first time only and then use the new ones the server sets, how can I do that?

the solutions I found don't use default cookies for first request.

Note: I can't login automatically to the website since it uses auth challenge so everytime I login manually and change those cookies only for first request and then when the server updates them I want to see this affects my current cookies.


Example of how my website works:

At first I login using recaptcha and then get temp cookies,

for the first request in my app I want to use these temp cookies (already know them)

later, which each request I need to use the cookies from the previous response (they change with each request)

My current code:

def main():
    start_time = time.time()
    keep_running = True
    while keep_running:
        keep_running = execute_data()
        time.sleep(5.0 - ((time.time() - start_time) % 5.0))


def execute_data():
    url = 'https:me.happ.com/rishum/register/confirm'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate',
        'Connection': 'close'
    }

    cookies = dict(rishum='dsfsdf21312zxcasd-' + '39480523')

    try:
        response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

You've almost got it but are a bit off on your dictionary implementation.

This is what you are looking for:

cookies = {
    "PHPSESSID": "djsgkjhsdjkhj34",
    "authchallenge" : "sdifhshdfiuh34234234",
    "rishum": 'skdhfuihuisdhf-' + '10403111'
    }

try:
    response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

Edit: I see now that this isn't the issue but that you want to update cookies during a session, here is a simple example of how to do so with requests.Session :

from requests import Session

s = Session()

s.cookies["foo"] = "bar"

r = s.get('https://google.com')

print("Before:")
for cookie in s.cookies:
    print(cookie)

print()

s.cookies["bo"] = "baz"
print("After: ")
for cookie in s.cookies:
    print(cookie)

Edit #2:

To further answer your question, here is a better example of how you can update cookies(all of them, if needed) in a loop.

from requests import Session, cookies

s = Session()

b = s.get('https://google.com')

for cookie in s.cookies:
    print(cookie.value)


# Iterate over cookies
for cookie in s.cookies:

    # You can see we already have this cookie's info in the $cookie variable so lets delete it from the cookie jar
    del s.cookies[cookie.name]
    
    # You can update the values HERE
    # ...
    # Example:
    cookieValue = cookie.value.upper()

    # Then save the new cookie to the cookie jar.
    updated_cookie = cookies.create_cookie(domain=cookie.domain,name=cookie.name,value=cookieValue)
    s.cookies.set_cookie(updated_cookie)

for cookie in s.cookies:
    print(cookie.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM