How to implement login on the website?

Good evening

Practiced logging into the forums, then came across a site with protection (token), protection seems to get around, but login still not implemented:

import requests
from bs4 import BeautifulSoup

url = "http://example.com"
loginurl = "https://example.com/users/sign_in"

headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0)Gecko/20100101 Firefox/51.0',
"Host":"example.com"}

s = requests.Session()
responsePageWithAuth = s.get(loginurl, headers = headers) #get the page users/sign_in to retrieve the token
#the process of obtaining will not show, just a shame :) (I foresee a bunch of people with instructions on correct code)


data = {"authenticity_token" : token,
 "email" : "name%40domen.ru",
"password":"12345",}

response = s.post(loginurl, data=data, headers=headers) #response with the result of the authorization
soup = BeautifulSoup(response.content, "html.parser") #readable form

print(soup.contents)


Get a page with error log. When you go to the browser in the headers was cookies, maybe they had something to do with login?
August 19th 19 at 23:27
1 answer
August 19th 19 at 23:29
Solution
cookies, maybe they had something to do with login?

And then!

Look that sends the browser when you login, including all headers, all cookies, all body POST and all the parameters in it (not that it needs to send based on the structure of forms, and that at this moment you actually send the scripts) and compare with what sends your parser.
Sometimes, it's easier to read scripts, understand their logic and rewrite in Python. Sometimes Vice versa, obfuscated script on a few thousand rows, then it is easier to analyze the network activity at the time of an action (login for example), to try different inputs to see how does it goes from entered, to identify patterns and to simulate such algorithm in Python.
from experience, the cookie requests he resolves in 99% of cases. most likely a problem with the sent data. - efrain.Rolfso commented on August 19th 19 at 23:32
Yes, in requests well implemented in session, but the scripts on the page can add/modify the cookie, then the next request is sent not what was received in the previous answer, and this logic live page is different from the way it mimics requests that sends all honest.
But I agree that it is not common. - Cora.Abshire commented on August 19th 19 at 23:35

Find more questions by tags Beautiful SoupParsingPython