Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to a website using python requests

I encounter some difficulties to log on a website from a python script, in order to retrieve data from it later on, once I will be connected. I think that the part of the HTML page with the form expecting username and password is the following :

<div class="contentLogin">
        <form action="/login/loginSubmit" method="post" class="memberLogin">
    <table cellpadding="0" cellspacing="0" border="0" >
        <tr>
            <td><label class="color2">Déjà membre</label></td>
            <td>&nbsp;</td>
            <td><input type="text" value="pseudo" class="input" name="login" id="login" /></td>
            <td><input type="password" value="pass" class="input" name="pass" id="pass" /></td>
            <td><input type="submit" value="ok" class="color2 loginSubmit" /></td>
        </tr>
        <tr>
            <td colspan="3"></td>
            <td colspan="2" >
                <a href="#" class="forgotPassword color2" id="forgotPassword">Mot de passe oublié ?</a>
            </td>
        </tr>
    </table>
</form>    </div>

I would like to use the "requests" module of python langage, to do the POST request that will connect me to the site. My code already contains the following commands :

import requests
pars = {'login': 'dva2tlse', 'pass': 'VeryStrong', 'action': 'Idunno'}
resp = requests.post("http://www.example.com", params=pars)

But it seems not to work, because I even do not know WHICH action should be indicated within th POST request. (I do not even know how exactly to use it, since I never done it) Thank' you to help me make all that work correctly, David

like image 937
dva2tlse Avatar asked Jun 20 '26 09:06

dva2tlse


1 Answers

Change the url value in requests.post to match the one given in <form action> attribute.

Also, remove the action key in your pars dictionary.

pars = { 'login': 'dva2tlse', 'pass': 'VeryStrong' }
resp = requests.post("http://example.com/login/loginSubmit", params=pars)

If you want to keep your login state for further page calls, you can use requests.Session()

s = requests.Session()
pars = { 'login': 'dva2tlse', 'pass': 'VeryStrong' }
resp = s.post("http://example.com/login/loginSubmit", params=pars)

As long as you keep using s, you will stay logged in.

like image 169
Jivan Avatar answered Jun 21 '26 22:06

Jivan