4

I have been googling for this problem for a week now. The thing I want to achive is the following:

Send a POST request to the URL including the correct credentials. Save the session (not cookie since my website is not using cookies at the moment) With the saved session open a session protected URL and grab the contents.

I have seen alot of topics on this with cookies but not with sessions, I tried sessions with requests but seems to fail everytime.

1 Answers1

2

You want to use a URL opener. Here's a sample of how I've managed to do it. If you just want a default opener, use opener=urllib.request.build_opener(), otherwise use the custom opener. This worked when I had to log into a website and keep a session, using URL as your URL, user as user, password as password, all changed as appropriate.

opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
pData=urllib.parse.urlencode({"identity":user,"password":password})
req=urllib.request.Request(URL,pData.encode('utf-8'))
opener.open(req)

req=urllib.request.Request(url)
response= opener.open(req)
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142