Python要求使用网站登录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python要求使用网站登录相关的知识,希望对你有一定的参考价值。
我似乎无法使用python requests.session()函数登录我的大学网站。我已经尝试检索登录所需的所有标题和cookie但它没有使用我的凭据成功登录。它没有显示任何错误,但我应该登录后查看的源代码显示它仍然没有登录。我被告知我需要使用csrf cookie信息,但我似乎无法动态检索csrf cookie数据因为它在我尝试检索时引发KeyError,就像我检索其他2个cookie信息一样。请帮助别人。
我的所有代码都在下面。我用我的凭据填写登录名和密码,但其余的都是确切的代码。
import requests
with requests.session() as r:
url = "https://www.ouac.on.ca/apply/nonsecondary/intl/en_CA/user/login"
page = r.get(url)
aspsessionid = r.cookies["ASPSESSIONID"]
ouacapply1 = r.cookies["OUACApply1"]
LOGIN = ""
PASSWORD = ""
submit = "Log In"
login_data = dict(ASPSESSIONID=aspsessionid, OUACApply1=ouacapply1, login=LOGIN, password=PASSWORD, submitButton=submit)
header = {"Referer":"https://www.ouac.on.ca/apply/nonsecondary/intl/en_CA/user/login", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"}
logged_in = r.post(url, data=login_data, headers=header)
new_page = r.get(url="https://www.ouac.on.ca/apply/nonsecondary/intl/en_CA/profile/")
plain_text = new_page.text
print(plain_text)
答案
我没有帐户,所以我无法测试它,但它可能是这样的。
import requests
import bs4
import webbrowser
def display(content):
# to see this HTML in web browser
with open('temp.html', 'wb') as f:
f.write(content)
webbrowser.open('temp.html')
with requests.session() as r:
LOGIN = ""
PASSWORD = ""
login_url = "https://www.ouac.on.ca/apply/nonsecondary/intl/en_CA/user/login"
profile_url="https://www.ouac.on.ca/apply/nonsecondary/intl/en_CA/profile/"
# session need it only once and it will remember it
r.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
})
# load page with form - to get cookies and `csrf` from HTML
response = r.get(login_url)
#display(response.content)
# get `csrf` from HTML
soup = bs4.BeautifulSoup(response.text, 'html.parser')
csrf = soup.find('input', {'name': 'csrf'}).attrs['value']
print('csrf:', csrf)
# cookies are not part of form so you don't use in form_data,
# session will use cookies from previous request so you don't have to copy them
form_data = {
'login': LOGIN,
'password': PASSWORD,
'submitButton': "Log In",
'csrf': csrf,
}
# send form data to server
response = r.post(login_url, data=form_data)
print('status_code:', response.status_code)
print('history:', response.history)
print('url:', response.url)
#display(response.content)
response = r.get(profile_url)
display(response.content)
以上是关于Python要求使用网站登录的主要内容,如果未能解决你的问题,请参考以下文章
python简单程序设计:许多计算机程序系统都需要登录,且要求账号密码输入错误不得超过三次?