python-2.7web-scrapingautologin

Autologin to website using python


I'm pretty new to python and am trying to auto-login to a website once there i will then try to download a file.

I have tried the below method and have not been able to login.

Method

import requests

Fill in your details here to be posted to the login form.

payload = {
    'username': 'xxxxxx',
    'password': 'xxxxxxx'
}

Use 'with' to ensure the session context is closed after use.

with requests.Session() as s:
    p = s.post('https://service.rl360.com/scripts/customer.cgi', data=payload)
# print the html returned to see if it's a successful login page.
print p.text


# An authorised request.
r = s.get('https://service.rl360.com/scripts/customer.cgi/SC/myAccount.php')
print r.text

Any help would be greatly appreciated, I'm using python 2.7


Solution

  • I assume you got the correct url to submit the form. One thing I noticed from that website is that

    <input type="text" name="USERNAME" id="username" title="please enter your username here" class="input" style="width: 14em;">
    

    I'm not sure how they wrote the server side code, but you may try using the name attribute with all capital characters instead of the id attribute, so your payload becomes : updated like below update, there is still one last hidden field in the form, maybe you should include it (the url includes that option value, but it is not in POST request, server side will not recognize it), you should also include user-agent string, just in case :

    payload = {
        'USERNAME': 'xxxxxx',
        'PASSWORD': 'xxxxxxx',
        'option': 'login'
    }
    headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
    def test():
      with requests.Session() as s:
          p = s.post('https://service.rl360.com/scripts/customer.cgi?option=login',data=payload)