MechanicalSoup是否可以登录到需要SAML身份验证的页面?

时间:2020-01-27 20:20:18

标签: python-3.x saml mechanicalsoup

我正在尝试从SSO(单一登录)站点的后面下载一些文件。似乎已通过SAML身份验证,这就是我遇到的问题。通过身份验证后,我将能够执行返回JSON的API请求,因此无需解释/抓取。

不确定如何在Mechanicalsoup中解决该问题(通常相对不熟悉Web编程),将不胜感激。

这是到目前为止我得到的:

import mechanicalsoup
from getpass import getpass
import json

login_url = ...
br = mechanicalsoup.StatefulBrowser()
response = br.open(login_url)
if verbose: print(response)

# provide the username + password
br.select_form('form[id="loginForm"]')
print(br.get_current_form().print_summary()) # Just to see what's there. 
br['UserName'] = input('Email: ')
br['Password'] = getpass()
response = br.submit_selected().text
if verbose: print(response)

这时,我得到一个页面,告诉我javascript已被禁用,必须单击提交以继续。所以我这样做:

br.select_form()
response = br.submit_selected().text
if verbose: print(response)

那是我抱怨状态信息丢失的地方。

输出:

<h2>State information lost</h2>

State information lost, and no way to restart the request<h3>Suggestions for resolving this problem:</h3><ul><li>Go back to the previous page and try again.</li><li>Close the web browser, and try again.</li></ul><h3>This error may be caused by:</h3><ul><li>Using the back and forward buttons in the web browser.</li><li>Opened the web browser with tabs saved from the previous session.</li><li>Cookies may be disabled in the web browser.</li></ul>

我发现在SAML登录背后进行抓取的唯一命中点都是采用硒方法(有时会下降到请求)。

机械汤可以吗?

1 个答案:

答案 0 :(得分:0)

事实证明,我需要Javascript才能登录。关于进入SAML身份验证的最初问题不是真正的环境。因此,这个问题尚未得到真正的解答。
感谢@Daniel Hemberger帮助我在评论中指出这一点。

在这种情况下,MechanicalSoup不是正确的工具(由于Javascript),我最终使用了selenium to get through authenication then using requests