How can I use python request to login this website?

Viewed 612

I'm quit new to Python and web-scraping. And I can't even achieve the first step of scraping a website: login. Before I try to use mechanize or selenium, I want to use requests first. Can someone help me?

The website I have been trying to log into is here.

For those who don't have an account and want to help me, I will provide the information I got by using Chrome.

The first POST: POST_1.JPG. The second POST: POST_2.JPG

And here is my code for the login attempt:

import requests

s = requests.Session()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
payload = {'username': 'USERNAME',
          'password': 'PASSWORD',
          'submit.x': '21',
          'submit.y': '12'}

s.post('https://ssologin.cuny.edu/oam/server/auth_cred_submit', data = payload, headers = headers)
target_page = s.get('https://home.cunyfirst.cuny.edu/psp/cnyepprd/EMPLOYEE/EMPL/h/?tab=DEFAULT', headers = headers)

if 'login' in target_page.url:
    print("Failed to Login")
else:
    print("Logged in Successfully!")

Thanks!!!

1 Answers
Related