简体   繁体   中英

Neopets login with Python Requests module

I am trying to login to this website with the following code:

import requests

def ReadNeopets(): 
    username = 'notarealone'
    LoginUrl = 'http://www.neopets.com/login/'
    with requests.Session() as s:
        s.get(LoginUrl)
        payload = {'destination': '', 'username': username, 'password': 'notrealeither'}
        print('Logging in to the site.')
        r = s.post(LoginUrl, data=payload)
        print(r.content)
        r = s.get('http://neopets.com/bank.phtml')
        Text = r.content.decode()
        print(r.cookies)
        if username in Text:
            print("Logged into my bank")
        elif bytes("Sign up") in Text:
            print("Failed to log in.")
   return;


def main():
    ReadNeopets()

if __name__ == "__main__":
    main()

Unfortunately it seems that the post does not work and when checking for the presence of the username it fails as the login was not successful or not attempted?

I am not certain what is happening here and would like to understand better as I am trying to move away from urllib and urllib2.

I added the cookie print and the text print to help try and understand if there were any errors I could try to comprehend but unfortunately not.

The post request goes to a different url, once you change the data as below your login will be successful:

    login_url = 'http://www.neopets.com/login.phtml'
    with requests.Session() as s:
        payload = {'destination': '', 'username': username, 'password': 'notrealeither'}
        r = s.post(login_url, data=payload)
        print(r.text)

If you want to see how a request works, open firebug or developer tools, for this request you can see it under the other tab:

在此输入图像描述

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