gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:33:49 25 4
gpt4 key购买 nike

我正在尝试使用 python 请求登录我大学的 CAS。我在网上看过一些教程和帖子,但它们都适用于在同一页面上具有用户名和密码字段的 CAS。这是我尝试登录的网站:https://cas.tamu.edu/cas/login?service=https://howdy.tamu.edu/uPortal/Login&renew=true

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

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

我基于本教程:https://brennan.io/2016/03/02/logging-in-with-requests/ 。特别是关于 CAS 的部分。

最佳答案

我找到了一种解决方法,虽然不完全是我想要的,但适合我的目的。我发布此内容以防有人发现此问题有类似问题。我使用 browsercookie 来使用 chrome 中的当前 cookie,因此如果我已经登录,那么我可以访问 CAS 后面的信息。更多信息请参阅这篇文章:https://stackoverflow.com/a/29628188/9613156 .

关于python - 如何使用python请求登录多个页面的CAS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49712525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com