Sep-09-2018, 06:34 AM
Hello, i'm trying to learn to use the requests module. I have written the following code, but it doesnt login to my account and returns an error.
Traceback (most recent call last):
File "python-io.py", line 12, in <module>
csrftoken = session.cookies['csrftoken']
File "/home/bunni/.local/lib/python2.7/site-packages/requests/cookies.py", line 328, in __getitem__
return self._find_no_duplicates(name)
File "/home/bunni/.local/lib/python2.7/site-packages/requests/cookies.py", line 399, in _find_no_duplicates
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrftoken', domain=None, path=None"
Kindly advice.
My project involves logging in to a website and searching for specific content which will then be downloaded and parsed to be presented in a clean format. I started learning python about 2 weeks back to achieve this. But i am stuck. Can you please tell me where i am going wrong and what i can do about it?
import requests url = 'https://python-forum.io/member.php?action=login' username = 'test' password = '*******' with requests.Session() as session: session.get(url) csrftoken = session.cookies['csrftoken'] login_data = dict(csrfmiddlewaretoken=csrftoken, username=username, password =password, next='/' ) session.post(url, data=login_data, headers={'Referer' : "https://python-forum.io"}) page = session.get('https://python-forum.io/usercp.php')Im getting the following error:
Traceback (most recent call last):
File "python-io.py", line 12, in <module>
csrftoken = session.cookies['csrftoken']
File "/home/bunni/.local/lib/python2.7/site-packages/requests/cookies.py", line 328, in __getitem__
return self._find_no_duplicates(name)
File "/home/bunni/.local/lib/python2.7/site-packages/requests/cookies.py", line 399, in _find_no_duplicates
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrftoken', domain=None, path=None"
Kindly advice.
My project involves logging in to a website and searching for specific content which will then be downloaded and parsed to be presented in a clean format. I started learning python about 2 weeks back to achieve this. But i am stuck. Can you please tell me where i am going wrong and what i can do about it?