简体   繁体   中英

Python requests returns login page

I want to retrieve some data from a page which has a login.

I use Python Requests for this. However when I try to login and then get the data from the data-page it returns the login page.

This is my code:

s = requests.Session()
s.auth = ('user', 'pass')
s.get('http://url.com')

payload = {'user': 'user', 'pass': 'pass'}
r_login = s.post("http://admin.url.com/login.php", data=payload)
cookies = r_login.cookies
print cookies

r_data = s.get('https://admin.url.com/stats.php?PHPSESSID={0}'.format(cookies['PHPSESSID']))
cookie = r_data.cookies
print cookie
print r_data.text

This is what is printed:

<<class 'requests.cookies.RequestsCookieJar'>[<Cookie PHPSESSID=ve4tit4svmp719ul3vb8qeqdh7 for .url.com/>]>
<<class 'requests.cookies.RequestsCookieJar'>[]>

And then the html of the login page instead of the stats page.

Am I forgetting something? How would I know what the server needs for authentication (ie cookies, sessionid, referrer)?

I assume from the title of your question that the get is returning the login page. The most likely reason for this is it does not have the session token so the web page is doing a server side redirect back to the login page.

My guess is that this (PHPSESSID) should be returned as a cookie and not on the query string.

Finally there might be some clever trick that I don't know but I imagine you will need to refer to the original code, documentation or trial and error to figure out what needs to be sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM