0

I am trying to log into website with my username and password but I just cannot seem to get it working. Right now I am testing by using the website. If I am able to log in, I should be able to find the bit that says The SnowBomb Platinum Membership. If I am not able to login, I should find the lost_password text in the source code.

This is what I have:

info = {
    'USERNAME' : 'username',
    'PASSWORD' : 'password',
    #'submit' : 'login'   don't know if i need this
}

def main():
    r = requests.post('http://www.snowbomb.com/my-account-2', data = info)  #logged in
    request = requests.get('http://www.snowbomb.com/my-account-2')
    if 'lost_password' in  request.content:
        print 'Was not able to log in'
        print 'lost_password' in request.content
    else:
        if 'The SnowBomb Platinum Membership' in request.content:
            print 'Logged in'
            print 'The SnowBomb Platinum Membership' in request.content #--> when it works

if __name__ == '__main__':
    main()

Here is the form in the source code when asking user to log in:

<form method="post" class="login">
            <p class="form-row form-row-first">
                <label for="username">Username <span class="required">*</span></label>
                <input type="text" class="input-text" name="username" id="username" />
            </p>
            <p class="form-row form-row-last">
                <label for="password">Password <span class="required">*</span></label>
                <input class="input-text" type="password" name="password" id="password" />
            </p>
            <div class="clear"></div>

            <p class="form-row">
                <input type="hidden" id="_n" name="_n" value="a046c51363" /><input type="hidden" name="_wp_http_referer" value="/my-account-2/" /><input type="hidden" name="redirect" value="http://www.snowbomb.com/my-account"/>
                <input type="submit" class="button" name="login" value="Login" />
                <a class="lost_password" href="http://www.snowbomb.com/wp-login.php?action=lostpassword&#038;redirect_to=http://www.snowbomb.com">Lost Password?</a>
            </p>
        </form>
5
  • Instead of these two lines: r = requests.post('http://www.snowbomb.com/my-account-2', data = info) request = requests.get('http://www.snowbomb.com/my-account-2'), use this: request = requests.get('http://www.snowbomb.com/my-account-2', auth = info) Commented Nov 26, 2013 at 13:51
  • @ZeinabAbbasi I get the error TypeError: 'dict' object is not callable Commented Nov 26, 2013 at 13:54
  • if 'The SnowBome Platinum Membership' in request.content: is that a typo you made just in the question, or are you checking for the wrong string in your code? Commented Nov 26, 2013 at 13:59
  • @NeilVass sorry that was a typo Commented Nov 26, 2013 at 14:05
  • I'm really sorry, I didn't realize that info is a dictionary; define info as a tuple, like this: info = ("username", "password"), then use the commands that I've provided before. Commented Nov 27, 2013 at 5:01

1 Answer 1

1

Whenever there is a successful login, the website sets cookies in the browser. We need to send these cookies with all further requests, only then the website will recognise you each time.

You can do this using urllib, urllib2 and cookielib. You can also do the same using requests and sessions.

import requests

s = requests.session()
url = "http://www.snowbomb.com/my-account-2"
login_request = s.post(url, data=payload)
print login_request.text
logged_in_request = c.get(url)
print logged_in_request.text
Sign up to request clarification or add additional context in comments.

4 Comments

would this still work if the login url and the protected url are the same? The website i am dealing with has 2 different views with the same URL depending if the user logged in or not
Almost all websites have same url, but different views for logged in and guest users. But are you sure, the url on which login request is posted is the same? Can you give me the website link, if its not a problem.
http://www.snowbomb.com/my-account-2 the html will change depending whether a user is logged in or not
@Liondancer Kindly close the question by accepting the answer, if it worked for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.