Daniel Guńka Daniel Guńka - 3 months ago 30
ASP.NET (C#) Question

Log in to ASP website using Python's Requests module

Im trying to webscrap some information from my school page, but im having hard time to get past login. I know there are similar threeds, i have spend whole day reading, but cannot make it work.

This is program im using (User name and password were changed):

import requests

payload = {'ctl00$cphmain$Loginname': 'name', 'ctl00$cphmain$TextBoxHeslo': 'password'}

page = requests.post('http://gymnaziumbma.no-ip.org:81/login.aspx', payload)
open_page = requests.get("http://gymnaziumbma.no-ip.org:81/prehled.aspx?s=44&c=prub")

#Check content
if page.text == open_page.text:
print("Same page")
else:
print(open_page.text)
print("Different page!")


Can you tell me, what im doing wrong? Am i missing some parameter? Is requests good metod for this? I was trying robobrowser and BeautifulSoup, but doesnt work either. I bet im missing something really trivial.

Im using Python 3.5

Answer

First off, you are not using a Session so even if your first post successfully logs you on the second knows nothing about it. Second, you are missing data that needs to be posted, __VIEWSTATEGENERATOR and __VIEWSTATE which you can parse from the source using BeautifulSoup:

from bs4 import BeautifulSoup

data = {'ctl00$cphmain$Loginname': 'name', 'ctl00$cphmain$TextBoxHeslo': 'password'}
# A Session object will persist the login cookies.
with requests.Session() as s:
    page = s.get('http://gymnaziumbma.no-ip.org:81/login.aspx').content
    soup = BeautifulSoup(page)
    data["___VIEWSTATE"] = soup.select_one("#__VIEWSTATE")["value"]
    data["__VIEWSTATEGENERATOR"] = soup.select_one("#__VIEWSTATEGENERATOR")["value"]
    s.post('http://gymnaziumbma.no-ip.org:81/login.aspx', data=data)
    open_page = s.get("http://gymnaziumbma.no-ip.org:81/prehled.aspx?s=44&c=prub")

    #Check content
    if page.text == open_page.text:
        print("Same page")
    else:
        print(open_page.text)
        print("Different page!")

You can see all the form data that gets posted in Chrome dev tools.

enter image description here

What is posted above should be enough to get logged in, if not any value you need can be parsed from the login table using BeautifulSoup.