0

This question has been addresses in various shapes and flavors but I have not been able to apply any of the solutions I read online.

I would like to use Python to log into the site: https://app.ninchanese.com/login and then reach the page: https://app.ninchanese.com/leaderboard/global/1

I have tried various stuff but without success... Using POST method:

import urllib
import requests
oURL = 'https://app.ninchanese.com/login'
oCredentials = dict(email='[email protected]', password='mypassword')
oSession = requests.session()
oResponse = oSession.post(oURL, data=oCredentials)
oResponse2 = oSession.get('https://app.ninchanese.com/leaderboard/global/1')

Using the authentication function from requests package

import requests
oSession = requests.session()
oResponse = oSession.get('https://app.ninchanese.com/login', auth=('[email protected]', 'mypassword'))
oResponse2 = oSession.get('https://app.ninchanese.com/leaderboard/global/1')

Whenever I print oResponse2, I can see that I'm always on the login page so I am guessing the authentication did not work.

Could you please advise how to achieve this?

1 Answer 1

1

You have to send the csrf_token along with your login request:

import urllib
import requests
import bs4

URL = 'https://app.ninchanese.com/login'
credentials = dict(email='[email protected]', password='mypassword')
session = requests.session()
response = session.get(URL)
html = bs4.BeautifulSoup(response.text)
credentials['csrf_token'] = html.find('input', {'name':'csrf_token'})['value']
response = session.post(URL, data=credentials)
response2 = session.get('https://app.ninchanese.com/leaderboard/global/1')
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Daniel and thanks a lot for taking the time to anwser! I have tried what you described above but when I print(response2.text) I have the following error message: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\alexis.rolland\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 5722-5724: character maps to <undefined>
Ok great! your solution actually worked and I was able to print by adding .encode("utf-8")... which gives me this: print(response2.text.encode("utf-8"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.