Im trying to webscrape 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
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')
soup = BeautifulSoup(page.content)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With