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> </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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With