Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why can't it not find this cookie?

I want to scrape this website: https://dbh.smartschool.be/ for a school project but I always run into a problem whit the authentication and I have no cleu why.
this is my code:

import requests

URL = "https://dbh.smartschool.be"
LOGIN_ROUTE = "/login"

HEADERS = {
   "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" ,
   "orgin" : URL,
   "referer" : URL + "/"
}

s = requests.session()

csrf_token = s.get(URL).cookies["PHPSESSID"]

login_payload ={
   "login_form[_username]": "Roel.bellemans",
   "login_form[_password]": "######",
   "login_form[_generationTime]": "1665576878",
   "login_form[_token]": csrf_token
}

login_req = s.post(URL + LOGIN_ROUTE, headers=HEADERS, data=login_payload)
print(login_req)

and this is what I get back:

Traceback (most recent call last):
  File "C:\Users\roelb\Desktop\cookie_auth_scraper\logIn.py", line 15, in <module>
    csrf_token = s.get(URL).cookies["PHPSESSID"]
  File "C:\Users\roelb\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 334, in __getitem__        
    return self._find_no_duplicates(name)
  File "C:\Users\roelb\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 413, in _find_no_duplicates
    raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}")
KeyError: "name='PHPSESSID', domain=None, path=None"

but there is a PHPSESSID
enter image description here
any thoughts?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your cookie issue falls on this line:

csrf_token = s.get(URL).cookies["PHPSESSID"]   # CAUSES ERROR

The cookie issue can be fixed with this:

import requests
URL = "https://dbh.smartschool.be"
s = requests.Session()
response = s.get(URL)
csrf_token = s.cookies.get_dict()['PHPSESSID']
print(csrf_token)

OUTPUT:

0ss1g37jsa5sg2sd2beu91vnqc8vamv3bmi03lmfc0p9c8kai3

If I leave out PHPSESSID : and print the dictionary instead like this:

csrf_token = s.cookies.get_dict()
print(csrf_token)

OUTPUT:

{'PHPSESSID': '0ss1g37jsa5sg2sd2beu91vnqc8vamv3bmi03lmfc0p9c8kai3', 'pid': 'e2163707-0803-49c8-af39-f8c499508845'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading