Login to site with POST request in python?

Asked

Viewed 3,343 times

3

I would like to log in to 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>

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 didn’t work, I think it’s because of the button

Can someone help me?

  • 2

    Don’t put never actual login data - prinicpaemtne password, no online resource - always replace the values you have with "*" or anything else.

  • 2

    Need to be using urllib? If not a necessity, you can do via Lenium.

  • 1

    For those who tried to edit to remove credentials: the right is to signal for moderation. Credentials remain visible in editing history if you just edit.

  • Just like @Deniscallau said, if you don’t need to use urllib, try using Selenium, I think it gets easier.

1 answer

1


I believe that the ideal would be through an API, usually these services provide API’s with procedures for access, the ideal would be to check this before. If unavailable, try Lenium.

Selenium

An alternative would be the 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 certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated as well.

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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.