Submit form in python

1

I'm trying to submit a form on link , but I've already tried several ways here in stackoverflow of how to submit a form, and found no way to solve this problem.

This is a code sample I took here in the forum and it did not 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 link , this implies that the form was not submitted successfully, and wanted to know what I'm doing wrong.

If you need the real login and password, please let me know without any problem

    
asked by anonymous 22.12.2017 / 14:30

2 answers

1
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 the authentication form
  • # 3 "Session Instance"
  • # 4 Do the POST method with the session object
  • # 5 Gets a slice of received text if authentication succeeds

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

    
22.12.2017 / 19:51
0

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

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 the form renders is the same - and this facilitates the creation of the data validation interface. But the author of a website can by the addresses you want, and if it is a system made by hand, without taking advantage of a framework code, it is simpler to have separate pages.

    
22.12.2017 / 19:43