Log in to ASP website using Python's Requests module
<p>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. </p>

<p>This is program im using (User name and password were changed):</p>

<pre><code>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&amp;c=prub")

#Check content
if page.text == open_page.text:
    print("Same page")
else:
    print(open_page.text)
    print("Different page!")
</code></pre>

<p>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.</p>

<p>Im using Python 3.5 </p>

<p>First off,  you are not using a <a href="http://docs.python-requests.org/en/master/user/advanced/#session-objects" rel="nofollow">Session</a> 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, <em>__VIEWSTATEGENERATOR</em> and <em>__VIEWSTATE</em> which you can parse from the source using <em>BeautifulSoup</em>:</p>

<pre><code>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&amp;c=prub")

    #Check content
    if page.text == open_page.text:
        print("Same page")
    else:
        print(open_page.text)
        print("Different page!")
</code></pre>

<p>You can see all the form data that gets posted in Chrome dev tools.</p>

<p><a href="http://i.stack.imgur.com/yxgky.png" rel="nofollow"><img src="http://i.stack.imgur.com/yxgky.png" alt="enter image description here"></a></p>

<p>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.</p>

python asp.net login web-scraping python-requests