Obtaining and formatting cookies from a request

Asked

Viewed 166 times

0

Guys, in PHP I used so:

preg_match_all('/Set-Cookie:\s([^\;]+)/mi', $response, $matches)

I took a look at the documentation and tried to follow this line of logic but it doesn’t work:

I’m an amateur with python and need to pick up some cookies from the request:

re.findall('/Set-Cookie:\s([^\;]+)/mi', response)

At the moment is python pure, has no framework involved yet, in the future I will use django.

the whole code is like this:

import requests
import re

response = requests.get('https://twitter.com')

print(re.findall('/Set-Cookie:\s([^\;]+)/mi', response))
  • William, so your question is how to pick up Cookies from a request and not how to use regular expressions in Python? It seems to be a XY problem. It would be interesting to you edit your question and add in tags which frameworks/libraries you are using and what is your ultimate goal.

  • I gave an edited @fernandosavio, at the moment is what I can explain oaksokas the request of twitter is Simbolica, I chose this url to study, could be google...

  • You are using the requests module.. it is already a library :-)

  • Would it be nice for you to check out documentation from the library, because she already takes care of it for you.

  • Yeah I’ve seen this documentation, but I’ll look more closely, vlw fernandosavio :)

  • 1

    Something like response.headers['set-cookie']. Much easier than extracting this with regular expressions.

  • @Andersoncarloswoss, yes but in the future I would need to extract, for example cookiename=valor; would need to extract only the value, so the interest to know regex in py

  • In PHP, bars only delimit regex, but are not part of it, and mi sane modifiers, that change some aspects of regex working. In Python it is different, so it would be something like re.findall(r'Set-Cookie:\s([^\;]+)', response, flags = re.I | re.M). Another point is that regex varies widely between languages, so not always what works in one will work exactly the same way in the other (this should not be the case, but it is important to know what each language supports before simply copying the regex)

  • I didn’t copy, I just gave the example of how I do in PHP. But vlw by the answer

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.