1

I'm getting error while i'm trying to fetch the csrftoken.
I tried changing the token name from csrftoken to csrf but i'm still getting the error.

import requests
url='myurl'
Username='username'
Password='password'

requests.Session().get(url)

csrftoken= requests.Session().cookies['csrftoken']
logindata=dict(csrfmiddlewaretoken=csrftoken,username=
Username,password=Password)

requests.Session().post(url,data=logindata,headers={'referer' :'homepage'})

page=requests.session().get('2nd url')
print page.content

The error is :-

File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packa es\requests\cookies.py", line 329, in __getitem__return self._find_no_duplicates(name)'

File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packa es\requests\cookies.py", line 400, in _find_no_duplicates raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path)) eyError: "name='csrftoken', domain=None, path=None"'

gervais.b
  • 2,294
  • 2
  • 22
  • 46
sandy9807
  • 21
  • 1
  • 5
  • and what about *domain=None, path=None*.. did you followup on the error message and provided required info? Moidfy your question such that requested response info is in it. Then your question is pretty much complete and shot in to the world-wide-web waiting for help responses ;-) – ZF007 Feb 20 '18 at 22:14
  • Thanx for replying but actually i'm new to python so i'm unable to actually detect the error i'm searching the web but not able to find why i'm getting the erorr. And i found out other library which is mechanicalsoup. will be trying to login with it and will update if i'm able to login. – sandy9807 Feb 20 '18 at 22:48
  • Hint... google or here at SO + search function + *eyError: "name='csrftoken', domain=None, path=None"'* = possible solution [>> here <<](https://stackoverflow.com/questions/37816858/cannot-login-to-website-using-requests-module-python-version-3-5-1). Or [here](https://www.google.nl/search?q=python+eyError%3A+%22name%3D%27csrftoken%27%2C+domain%3DNone%2C+path%3DNone%22%27&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&dcr=0&ei=L6aMWuKdLpDA8gec2JSAAQ). – ZF007 Feb 20 '18 at 22:52
  • Thanx for the help.i'll se if i can get it – sandy9807 Feb 20 '18 at 22:57
  • actually i had checked the webpage html code there were no hidden element.and also can you pls tell me how can i find the cookie name.coz is it that i'm putting the cookie name wrong and hence getting the error – sandy9807 Feb 20 '18 at 23:05
  • Sorry but I've never worked with cookies and python-requests like this. But I know how to search... check [here](https://stackoverflow.com/questions/13567507/passing-csrftoken-with-python-requests) and read the comments from Martijn Pieters about cookies, servers in debug mode, etc. – ZF007 Feb 20 '18 at 23:10
  • ok i'll see..... – sandy9807 Feb 20 '18 at 23:15
  • but in my case i'm not able to execute the step in which the referer is given i'm getting the error before that step. which is this line "csrftoken= requests.Session().cookies['csrftoken']" – sandy9807 Feb 20 '18 at 23:19
  • can you pls help me finding the csrf token name...?. coz i have searched the whole html code but was not able to find it – sandy9807 Feb 20 '18 at 23:21

1 Answers1

0

This code evades the Session part of requests version with capital "S". Try to see if this works for you:

import requests

url = 'url'
client = requests.session()
# Retrieve the CSRF token first
csrf = client.get(url).cookies['csrftoken']

login_data = dict(username=EMAIL, password=PASSWORD, csrfmiddlewaretoken=csrf, next='/')
r = client.post(URL, data=login_data, headers=dict(Referer=url))

# Check if it worked?
print r.status_code

Perhaps its bugged at your install?

EDIT1:

try this:

import requests
from bs4 import BeautifulSoup

client = requests.Session()
headers = {'User-Agent': 'Mozilla/5.0'}
url = 'url'

soup = BeautifulSoup(client.get(url).text, "html.parser")
csrf = soup.find(name="csrf")
ZF007
  • 3,708
  • 8
  • 29
  • 48
  • i'm getting the same error.As per my understanding i think that the error is caused just because i'm not giving the correct token name,everything else is fine – sandy9807 Feb 20 '18 at 23:34
  • Then I'm out of options... so you have to wait for the cavalry to help you out. GL! – ZF007 Feb 20 '18 at 23:38
  • 1
    so now trying a workaround with selenium coz i searched that it gives the browser instance. hope it works.....btw thanx for your efforts. – sandy9807 Feb 20 '18 at 23:39
  • 1
    had tried to find by using soup earlier before posting but no luck.......anyways thnx again man.Appreciate your efforts...! – sandy9807 Feb 20 '18 at 23:45