python请求具有重定向的模块

时间:2017-08-30 17:50:13

标签: python http python-requests

我正在尝试使用requests模块在python中执行get请求。但是,在我可以做之前,网站会将我重定向到登录页面。我需要先登录,然后登陆我到我要求的页面。

以下是我在获取后收到的内容。我应该如何执行登录才能访问我要查找的页面?任何帮助,将不胜感激!

         <form action="/idp/profile/SAML2/Redirect/SSO?execution=e1s1" method="post">


            <div class="form-element-wrapper">
                <label for="username">Username</label>
                <input class="form-element form-field" id="username" name="j_username" type="text" value="">
            </div>

            <div class="form-element-wrapper">
                <label for="password">Password</label>
                <input class="form-element form-field" id="password" name="j_password" type="password" value="******">
            </div>

            <div class="form-element-wrapper">
                <input type="checkbox" name="donotcache" value="1">Don't Remember Login  </div>

            <div class="form-element-wrapper">
                <input id="_shib_idp_revokeConsent" type="checkbox" name="_shib_idp_revokeConsent" value="true">
            Clear prior granting of permission for release of your information to this service.              
            </div>
            <div class="form-element-wrapper">
              <button class="form-element form-button" type="submit" name="_eventId_proceed"
                onClick="this.childNodes[0].nodeValue='Logging in, please wait...'">Login</button>
            </div>
        </form>

以下是我到目前为止编写的代码:

values = {'j_username':'****'}
with requests.Session() as s:
    p = s.get(url,verify=False)
    logger.info(p.text)

1 个答案:

答案 0 :(得分:0)

values = {'j_username':'****'}
with requests.Session() as session:
    login_response = session.post(login_url, data=data, verify=False)
    # the session will now have the session cookie, so subsequent requests will be authenticated. its worth inspecting the response to make sure it is the correct status code.
    other_response = session.get(url)  # expect this not to redirect to login page