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

时间:2018-09-09 15:14:21

标签: python-3.x python-requests

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

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

1 个答案:

答案 0 :(得分:1)

刚刚为您的code sample找到了解决方案:您必须设置cookie和身份验证。

这是工作代码:

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)