A
A
Alexander2020-09-21 23:43:42
Python
Alexander, 2020-09-21 23:43:42

How to use python,requests to login to a website?

I have been trying for the second day to write an authorization that works on this site. On others it works, but on this I don’t even see the reason for the failure. The site is powered by moodle.

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'}

login_data = {'anchor': None,
              'logintoken': None,
              'username': 'guest',
              'password': 'guest'
              }

with requests.Session() as s:
    url = 'http://www.uetk-spo.ru/login/index.php'
    r = s.get(url, headers=headers)
    soup = BeautifulSoup(r.content, 'html5lib')
    login_data['logintoken'] = soup.find(
        'input', attrs={'name': 'logintoken'})['value']
    f = s.post(url, data=login_data, headers=headers)
    print(f.content)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2020-09-22
@LolStriker56

And what is wrong? The script successfully logs in. It remains to save the received cookies and pass them in subsequent requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question