Using Python Requests to pass through a login/pass

2019-07-10 21:26发布

问题:

I have looked at related answers and I haven't found something that quite works. I'm trying to scrape some fantasy baseball information from my team's CBS Sportsline page.

I want to post the login and password and then when I use the get command, see the data specific to my account.

Here's what I attempted:

import requests

myurl = 'http://bbroto.baseball.cbssports.com/transactions'

payload = {
       'userid': '(my username)',
       'Password': '(my password)',
       'persistent': '1'}

session = requests.session()
p = requests.post(myurl, data=payload)

r = session.get(myurl)

print r.content

it gives me the generic page (as if I'm not logged in).

Here's what the form information looks like:

<form method="post" action="/login/index" name="login_form" id="login_form" >
<input     type="hidden" name="dummy::login_form" id="dummy::login_form" value="1">
<input type="hidden" id="form::login_form" name="form::login_form" value="login_form">
<table id="table_login_form" class="formLayout"><input type="hidden" 
value="http://bbroto.baseball.cbssports.com/transactions" name="xurl" id="xurl"><input
type="hidden" value="150" name="master_product" id="master_product"><input 
type="hidden" value="cbssports" name="vendor" id="vendor"><tr id="row_userid"><td 
align="right"></td>
<td><table class="fieldLayout"><tr><td><input type="text" value="Email or 
ID" name="userid" id="userid" class="placeHolder" onfocus="if(this.value==defaultValue)
{this.value='';};" size="30" maxlength="264" data-field-placeholder="Email or ID" data
-field-required="1"></td></tr></table></td></tr><tr id="row_password"><td    
align="right">
</td><td>
<table class="fieldLayout"><tr><td><input type="password"
value="Password" name="password" id="password" class="placeHolder" 
onfocus="if(this.value==defaultValue){this.value='';};" size="30" maxlength="12" data-
field-placeholder="Password" data-field-required="1"></td></tr></table></td></tr><tr>   
<td>
<!--label column--></td><td colspan="5"><input type="submit" id="submitButton"  
value="Sign
In" class="formButton" om-event="User Utilities|Sign In - Member|Sign In"></td></tr>
</table><!--end FORM table-->
</form>

So it seems the relevant fields are userid and Password

回答1:

You posted wrong form data.

Look at the post data:

So the post data may look like this:

payload = {  
   'dummy::login_form': 1,  
   'form::login_form': 'login_form',  
   'xurl': '/',  
   'master_product': 150,  
   'vendor': 'cbssports',  
   'userid': YOUR EMAIL HERE,  
   'password': YOUR PASSWORD HERE,  
}