Answer the question
In order to leave comments, you need to log in
How to get cookies in requests, correctly save to a file, then use?
Hello, from time to time I will need to run a script that will parse pages NOT of my site for some time. Since authorization on the site will not let me go deeper, I need to log in to the site and go deeper. For this I use Requests Session. My parsing script is ready, however, if I periodically log in and log in, and log in again, the admins are not fools, they will burn. Then I thought about security, it turns out I need to somehow log in and not be burned by the fact that I log in, and for this there are cookies. But there is a problem with them. If, after logging in to the site, use the command
cookie1 = requests.utils.dict_from_cookiejar(session.cookies)
{'csuid': 'udP***********1IAxB7Ag==', 'xf_csrf': 'Ebb********NgXA', 'xf_session': '********TCl81zu1n******Hmk', 'xf_user': '9*************************a7RkNmzIVn3OTfs'}
cookie2 = session.cookies
<RequestsCookieJar[<Cookie csuid=udP***********1IAxB7Ag== for .uc.zone/>, <Cookie xf_csrf=Ebb********NgXA for uc.zone/>, <Cookie xf_session=********TCl81zu1n******Hmk for uc.zone/>, <Cookie xf_user=9*************************a7RkNmzIVn3OTfs for uc.zone/>]>
try:
session.cookies.set_cookie(cookie1)
except:
session.cookies.set_cookie(cookie2)
Answer the question
In order to leave comments, you need to log in
something like this:
import requests, json
session = requests.session()
session.get('https://httpbin.org/cookies/set/my-cookie/my-value')
print(session.get('https://httpbin.org/cookies').json())
with open('cookies.json', 'w') as f:
json.dump(requests.utils.dict_from_cookiejar(session.cookies), f)
session = requests.session()
print(session.get('https://httpbin.org/cookies').json())
with open('cookies.json') as f:
session.cookies.update(json.load(f))
print(session.get('https://httpbin.org/cookies').json())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question