python解决问题asp,使用Python的“请求”模块登录ASP网站

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

解决方案

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.

87e0ff4c39c8e2c1cc93356a167e7e3b.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值