0

I am new to Python and I'm trying to log into a website using the Requests module and print out some of my account information. See code below :

import requests

username = 'myusername'
password = 'mypassword'
URL = 'https://www.mmoga.com/login.php'
payload = {'email_address': username, 'password': password}

session = requests.session()
r = requests.post(URL, data=payload)
account = session.get('https://www.mmoga.com/shopping_cart.php')
print account.content

Do I also have to add headers to the requests.post?

When I look at the html source I am seeing

<div class="boxHeading">
  <a href="mmoga.com/login.php"; title="- My Account -">- My Account -</a>
</div> 

however I should be seeing

<div class="boxHeading">
  <a href="mmoga.com/account.php"; title="- My Account -">- My Account -</a>
</div>
4
  • Could you let us know what response you get, and how it was different from your expectations? Commented Oct 16, 2015 at 13:05
  • Have you read this ... docs.python-requests.org/en/v2.0-0/user/authentication Commented Oct 16, 2015 at 13:07
  • @EricWilson When I look at the html source I am seeing <div class="boxHeading"><a href="mmoga.com/login.php" title="- My Account -">- My Account -</a></div> however I should be seeing <div class="boxHeading"><a href="mmoga.com/account.php" title="- My Account -">- My Account -</a></div> Commented Oct 16, 2015 at 13:11
  • I've added that to the question. Commented Oct 16, 2015 at 13:17

2 Answers 2

1

When posting the the login form on that page you also need to pass a query string with a "get" parameter named action:

import requests

username = 'myusername'
password = 'mypassword'
URL = 'https://www.mmoga.com/login.php'
payload = {'email_address': username, 'password': password}
params = {'action': 'process'}

session = requests.session()
r = session.post(URL, data=payload, params=params)
account = session.get('https://www.mmoga.com/shopping_cart.php')
print account.content

That should fix the login problem. Also, the session is not being used during login, so you should use session.post() as shown above.

Sign up to request clarification or add additional context in comments.

Comments

1

Comments

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.