Help with automating web login using requests


3 posts
by sroof » Mon Mar 20, 2017 9:45 pm
So I'm trying to following seemingly simple code to login to a password-protected site using Python and "requests". When I run this code, the final line returns the original login site, not the site I should have landed on following a successful login (and it's not the site that follows an invalid login). The login site does NOT have any hidden fields that I can find in its source code.
Can anyone point out what is going wrong?

Code: Select all
#!/usr/bin/env python
import requests

values = {'username':'myusername', 'password':'mypassphrase'}
r = requests.post('https://emoncms.org/user/login', data=values)
print r.url    #prints URL of destination site after login


I've also tried this variant but get the same result:
Code: Select all
r = requests.get(‘https://emoncms.org/user/login', auth=('myusername', 'mypassphrase'))
Posts: 12
Joined: Sun Jan 12, 2014 2:10 am
by Ernst » Mon Mar 20, 2017 9:58 pm
Why so difficult if you can use the API ?

https://emoncms.org/site/api#input
My first computer was an ICT1500
Posts: 283
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany
by sroof » Tue Mar 21, 2017 12:47 am
Ernst wrote:Why so difficult if you can use the API ?

https://emoncms.org/site/api#input


Good question. My actual target URL is a different one that I can't share and it has no API.
Posts: 12
Joined: Sun Jan 12, 2014 2:10 am