0

I want to start the script to login into one website and store the cookies in some file, and later i wanna start the script to visit other pages of that website but with same cookies, so I do not have to login everytime for everypage that I wanna visit:

import requests
import random
import sys
link = sys.argv[1]
link = link.replace("%26", "&")
link = link.replace("######", "&")
if len(sys.argv) > 3 :
    username = sys.argv[2]
    password = sys.argv[3]
else:
    username = ''
    password = ''
###LOGIN PART###
payload = {
    'username': username,
    '_spring_security_remember_me': '',
    'password': password,
    'targetUrlNonEncoded': 'false',
}

UAS = (
       "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36",
       )
ua = UAS[random.randrange(len(UAS))]
headers = {'user-agent': ua}
s = requests.Session()
r = s.post(link, headers=headers,data = payload)
print(r.text.encode('utf8'))

I did this and it works great, can someone help me how from this i can save the cookies and use them again later?

  • 1
    Does this answer your question? [How to use cookies in Python Requests](https://stackoverflow.com/questions/31554771/how-to-use-cookies-in-python-requests) – Mehdi Mostafavi Aug 26 '20 at 15:18
  • no it is not helpful –  Aug 26 '20 at 17:05
  • you could always store the payload in a json file and then when you run the script again, check if the json file exists, and load the information from it and use the information as the payload – ewokx Aug 27 '20 at 01:31
  • ewong, yes that is the idea, but how ti implement it? –  Aug 27 '20 at 12:36

0 Answers0