V
V
Vincent2019-04-16 19:27:45
Python
Vincent, 2019-04-16 19:27:45

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)

Then in cookie1 will be
{'csuid': 'udP***********1IAxB7Ag==', 'xf_csrf': 'Ebb********NgXA', 'xf_session': '********TCl81zu1n******Hmk', 'xf_user': '9*************************a7RkNmzIVn3OTfs'}

And if this command cookie2 = session.cookies
Then in cookie2 will be
<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/>]>

OK. Received, let's say saved (We still need to return to this) to a file, launched the script, the script found a file with cookies (no matter what) using the command
try:
    session.cookies.set_cookie(cookie1)
except:
    session.cookies.set_cookie(cookie2)

I'm trying to insert cookies, but in the first case it throws
AttributeError("'dict' object has no attribute 'value'",)
In the second
AttributeError("'RequestsCookieJar' object has no attribute 'value'",)
What's wrong with requests? He himself gave these cookies, how can I use them?
Another question is how to save cookies, I tried it in json files, but to no avail, then he can’t read them normally

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dgk, 2019-05-04
@dgk

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 question

Ask a Question

731 491 924 answers to any question