简体   繁体   English

使用Python的Requests模块登录ASP网站

[英]Log in to ASP website using Python's Requests module

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.我正在尝试 robobrowser 和 BeautifulSoup,但也不起作用。 I bet im missing something really trivial.我敢打赌我错过了一些非常微不足道的东西。

Im using Python 3.5我使用 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 :其次,您缺少需要发布的数据__VIEWSTATEGENERATOR__VIEWSTATE ,您可以使用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.您可以看到在 Chrome 开发工具中发布的所有表单数据。

在此处输入图片说明

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.上面发布的内容应该足以登录,如果没有您需要的任何值,可以使用 BeautifulSoup 从登录表中解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM