如何使用python请求使用多个页面登录CAS?

如何使用python请求使用多个页面登录CAS?,python,python-requests,cas,Python,Python Requests,Cas,我正在尝试使用python请求登录我所在大学的CAS。我在网上看到了一些教程和帖子,但它们都适用于在同一页面上有用户名和密码字段的CAS。以下是我尝试登录的网站: 如您所见,只有一个用户名字段,然后单击“提交”后,您将进入同一页面,现在只有该字段是密码字段。以下是我现有的不起作用的代码: import requests import lxml.html from bs4 import BeautifulSoup # URL of webpage login_url = "https://cas

我正在尝试使用python请求登录我所在大学的CAS。我在网上看到了一些教程和帖子,但它们都适用于在同一页面上有用户名和密码字段的CAS。以下是我尝试登录的网站:

如您所见,只有一个用户名字段,然后单击“提交”后,您将进入同一页面,现在只有该字段是密码字段。以下是我现有的不起作用的代码:

import requests
import lxml.html
from bs4 import BeautifulSoup

# URL of webpage
login_url = "https://cas.tamu.edu/cas/login?service=https://howdy.tamu.edu/uPortal/Login&renew=true"
howdy = "https://howdy.tamu.edu/uPortal/normal/render.uP"
username = # my username
password = # my password

# create a session to store cookies
sesh = requests.session()

params = {'service': howdy}
# gets the URL and converts the text of the HTML code
req = sesh.get(login_url, params=params)
html_content = req.text

print html_content

# parsing the page for hidden inputs
login_html = lxml.html.fromstring(html_content)
hidden_inputs = login_html.xpath(r'//form//input[@type="hidden"]')
user_form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
print(user_form)

user_form["username"] = username
user_response = sesh.post(login_url, data=user_form)

print user_response.url

# same thing for the password page
pass_form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
print(pass_form)

pass_form["password"] = password
pass_response = sesh.post(user_response.url, data=pass_form)

print pass_response.url

我根据本教程得出此结论:。特别是关于CAS的部分。

我发现了一个解决方案,它不是我想要的,但适合我的目的。我发布这个,以防有人发现类似的问题。我使用browsercookie来使用来自chrome的当前Cookie,因此如果我已经登录,那么我可以访问CAS后面的信息。此帖子中的更多信息: