Submit form in python

I am trying to submit a form on the website https://www.loskatchorros.com.br/ucp/login.php , but I have looked for several ways here in stackoverflow How to submit a form, and I did not find any way to solve this problem.

This is an example code I picked up here on the forum and it didn't work.

import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'login':'*******','senha':'******'}

session = requests.Session()
r = session.post('https://www.loskatchorros.com.br/ucp/login.php',headers=headers,data=payload)

When placing the line:

print (r.text)

The program returns the same source code as the page https://www.loskatchorros.com.br/ucp/login.php , this gives to understand that the form was not submitted successfully, and wanted to know what I'm doing wrong.

If you need the true login and password ask that I send without any problem

Author: BrTkCa, 2017-12-22

2 answers

import requests

account = {'login': '[email protected]', 'senha': 'muze123'}
abs_domain = 'https://www.loskatchorros.com.br/' #1
form = 'ucp/global/verifica_login.php' #2
absolute_form_URL = abs_domain + form 

requests_session = requests.Session() #3
r = requests_session.post(url=absolute_form_URL, data=account) #4

text = r.text[-79:] if r.status_code == 200 else None #5
print('Status code [%s].' % r.status_code, text, sep='\n')
  • # 1 absolute domain
  • #2 Path to authentication form
  • #3 "instance the session"
  • # 4 makes the POST method with the session object
  • # 5 gets a slice of the received text if authentication is successful

>>> Status code [200].
>>> Usuário ou senha incorretos, verifique e tente novamente!</div></body> </html>

 1
Author: Franklin Timóteo, 2017-12-22 18:51:46

You have to send the post with the data to the landing page of the form, not to the page that generates the form in HTML.

The URL for this page is the one in the "action" field of the form tag. In this case, global/verifica_login.php (and not login.php).

In modern web frameworks in general the URL that generates the html of a form and what processes the form is the same - and this makes it easier to create the data validation interface. But the author of a site can by the addresses he wants, and if for a system made by hand, without taking advantage of code from a framework, it is simpler to have separate pages.

 0
Author: jsbueno, 2017-12-22 18:43:56