登录网站后向URL发送请求时出错(Python请求)

问题描述 投票:1回答:1

我正在尝试使用请求登录网站后向URL发送请求。该URL是初始网站的子级。

Here is the code so far。任何帮助都很棒,谢谢。

python-3.x python-requests
1个回答
1
投票

刚为您的code sample找到解决方案:您必须设置cookie和auth。

这里的工作代码:

from requests import Session

# Create headers dict.
headers2 = {
    # Your headers.
}


Post_Login_URL = 'http://parents.netherhall.org/'
Request_URL = 'https://parents.netherhall.org/parents/students/?admissionno=011161&page=homework'
payload = {
    'username': 'your_username',
    'password': 'your_password'
}


with Session() as session:
    # Send post request to login.
    response = session.post(Post_Login_URL, headers=headers, data=payload)
    cookies = dict(response.cookies)
    # print(post.text) # Page source.
    response = session.get(Request_URL, headers=headers, auth=(r'your_username', 'your_password'),
                           verify=False, cookies=cookies)
    print('Logged in successfully:', response.ok)
© www.soinside.com 2019 - 2024. All rights reserved.