How to log in and get any page from a python site?


I have the following code for authorization. But it gives me the same html twice, but I need to first give out the authorization page, and then the one I am requesting. How can I resolve this? It's all about cookies, as I understand it.

import requests
from bs4 import BeautifulSoup

EMAIL = '***'
PASSWORD = '***'

def login(url):
    values = {'email': EMAIL,
              'password': PASSWORD
    page =, data=values)

def get_html(url):
    responce = requests.get(url)
    return responce.content

def parse(html):
    soup = BeautifulSoup(html)

def main():
    if login('http://***.**.***.**'):

if __name__ == "__main__":


First, as the comments suggest, create a session object:

def main():
    session = requests.Session()

Pass it on:

    if login('http://...', session):

In the login function, you need to check whether the authorization was successful. You can answer code:

def login(url, session):
    values = {'email': EMAIL,
              'password': PASSWORD
    page =, data=values)
    return page.status_code == 200

And in main we will add else:

    if login('http://...'):
        print(get_html('http://...', session))
        print('authorization failed')

Because most likely the problem is her. And how to make a normal authorization – you need to look at how it works on a specific site

Scroll to Top