S
S
sddvxd2017-01-26 21:33:30
Python
sddvxd, 2017-01-26 21:33:30

How to login on the site?

Good evening
I practiced with the entrance to the forums, then I came across a site with protection (token), I seem to bypass the protection, but the login is still not carried out:

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) #получаю страницу users/sign_in для извлечения токена
#сам процесс получения не покажу, стыдно просто :) (предвижу кучу людей с наставлениями по правильному коду)


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

response = s.post(loginurl, data=data, headers=headers) #ответ с результатом авторизации
soup = BeautifulSoup(response.content, "html.parser") #читаемый вид

print(soup.contents)

I get a login error page. When I logged in from the browser, there were cookies in the headers, maybe they are somehow connected with the login?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2017-01-26
@sddvxd

cookies, maybe they are somehow connected with the login?

And then!
See what the browser sends at the time of login, including all headers, all cookies, the entire POST body and all the parameters in it (not what it should send based on the form structure, but what the scripts actually send at that moment) and compare with what sends your parser.
Sometimes it's easier to read scripts, understand their logic and rewrite them in python. It happens on the contrary, an obfuscated script for several thousand lines, then it is easier to analyze network activity at the time of the action (login, for example), try different input options, observe how the transmitted depends on the input, identify patterns and imitate a similar algorithm in python.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question