Login to site with POST request in python?

Question:

I would like to log into this site http://www.ciee.org.br/portal/LOGIN.ASP

where your form:

<form name="frmLogin" method="post" action="validalogin.asp" >                  
          <span>Login:</span>
          <span>
          <input title="Informe seu login (inclusive LETRAS e '*' quando houver)" name="usuario" type="text" size="20" maxlength="70" onFocus="limpa()">
          </span>
          <span>Senha:</span>
          <span>
          <input title="Informe a senha fornecida pelo CIEE" name="passwd" type="password" value="" size="15" maxlength="15"></span>
          <br/>
         <center><input type="button" value="ok" onClick="document.frmLogin.submit();" style="cursor:pointer;">
         </center>
  </form>

tried the following:

import urllib

url = 'http://www.ciee.org.br/portal/LOGIN.ASP'
parametros = urllib.urlencode({'usuario':'usr', 'passwd': 'psw'})
html = urllib.urlopen(url, parametros).read()
print html

but it didn't work, I think it's because of the button

Can someone help me?

Answer:

I believe that the ideal would be through an API, generally these services provide API's with procedures for access, the ideal would be to check this before. If not available, try selenium.

selenium

An alternative would be selenium , site description (free translation and adaptation):

Selenium automates browsers. That's it! What you do with this power is entirely up to you. Mainly to automate web applications for testing purposes, but it's certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated too.

Example of access to facebook (just adapt to your case):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Login/senha
user = "nome_user"
pwd = "senha"

# Inicializacao do driver Firefox
driver = webdriver.Firefox()

# Abrindo a pagina do facebook
driver.get("http://www.facebook.com")

# Entrada do nome do usuário / email
elmnt = driver.find_element_by_id("email")
elmnt.send_keys(user)

# Entrada da senha
elmnt = driver.find_element_by_id("pass")
elmnt.send_keys(pwd)

# Pressionando o botão de login
elmnt.send_keys(Keys.RETURN)

# Fechando o driver
driver.close()
Scroll to Top
AllEscort