H
H
HexUserHex2020-06-22 12:07:00
Python
HexUserHex, 2020-06-22 12:07:00

What is the easiest way to load cookies in python from a file and send using request?

Good afternoon,
there are cookies that I saved from Chrome in this format to a txt file, tell me how easy it is to download and send them to the server using requests.get?:

Cookie: datadome=Nltdn1k_lnFG_rky3QGp8Hq3KcUZ5USom1jJQUByl5l.3K8lonS6wojP7-pEroLCCa_x0Xi0WpvgacC4brOf9JdAx4-Dtnf8etagR-zVt; akavpau_FRPRD_FNACCOM=1592812565~id=19bff601c627f808a3687c542c305286; ORGN=FnacAff; OrderInSession=0; UID=09631e55c-1a42-444d-8869-8126ebec29fb; SID=e9a3eb88-837f-468f-a4a2-d1cad982b233; RT="z=1&dm=fnac.com&si=xm6o3wur6h8&ss=kbpcg8vx&sl=0&tt=0"; s_cc=true; s_fid=6068BB924002F47E-1114F404BF09D6EF; campaign=FnacAff; s_ev14=%5B%5B%27FnacAff%27%2C%271592760481303%27%5D%5D; s_sq=%5B%5BB%5D%5D; test=V120206211327360.8179053883732972; OptanonConsent=isIABGlobal=false&datestamp=Sun+Jun+21+2020+13%3A29%3A49+GMT-0400+(EDT)&version=6.1.0&consentId=07592c64-7fd3-4189-b670-a98affb20ee1&interactionCount=2&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0004%3A1%2CC0003%3A1&hosts=&legInt=&geolocation=%3B&AwaitingReconsent=false; s_vi=[CS]v1|2F77CC448515874B-400006CE97ADBB0F[CE]; s_ev61=%5B%5B%27Non%2520internal%2520campaign%27%2C%271592760481293%27%5D%2C%5B%27Non%2520internal%2520campaign%27%2C%271592760481304%27%5D%5D; OptanonAlertBoxClosed=2020-06-21T17:27:37.372Z; consentNoticeClosed=accept; eb-profile=2d57772f-8464-4ff6-80b3-8694e4d58cd8:-258574946:1592760457740; rlvt_tmpId=ckbpcgavt00001w9ybp0iuh5h; etuix=__TodzlqHYj2EfBeLYJ6ysiR3TwKIK19t3CqPwKsTbQwH5bqhW869g--; _gcl_au=1.1.951391566.1592760459; sto__vuid=09631e55c-1a42-444d-8869-8126ebec29fb; _fbp=fb.1.1592760460424.617212303; CRTOABE=0; mics_vid=8928634453; mics_lts=1592760461150; _hjid=2570536b-1952-4093-a525-f1da41397078; kvn=Va; kameleoonVisitorCode=_js_q3ow5xdpwhxulzos


how i try:
cj = http.cookiejar.MozillaCookieJar('cookies.txt')                                                       
cj.load()                                                                                                       
contents = requests.get(url, cookies=cj)


I get:
http.cookiejar.LoadError: 'cookies.txt' does not look like a Netscape format cookies file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2020-06-22
@HexUserHex

You can make a separate function:

def cookies_from_file(filename):
  with open(filename) as file:
    cookies_data = file.read().replace('\n','').split(';')
  cookies_dic = {}
  for x in cookies_data:
    key = x.split('=')[0]
    value = x.split('=')[1]
    cookies_dic[key]=value
  return cookies_dic

cookies = cookies_from_file('cookies.txt')
response = requests.get(url,headers=headers,cookies=cookies)

I just checked it on the toaster - it works)
PS: It is advisable to delete the node of the cookie file at the beginning of "Cookie: "

D
Dimonchik, 2020-06-22
@dimonchik2013

import requests
s = requests.session()
s.get('https://www.google.com', cookies = {'cookieKey':'cookieValue'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question