Login to site with POST request in python?

3

I would like to log in to this site link

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>

I 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 did not work, I think it's because of the

Can anyone help me?

    
asked by anonymous 19.06.2017 / 21:31

1 answer

1

I believe the ideal would be through an API, usually these services provide API's with procedures for access, ideally it would check this before. If you do not have it, try the selenium.

Selenium

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

  

Selenium automates browsers. That's it! What you do with that power depends entirely on you. Primarily, to automate web applications for testing purposes, but certainly not limited to just that. Web-boring administration tasks can (and should!) Be automated as well.

Example of facebook access (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()
    
20.06.2017 / 17:13