2017-08-30 76 views
0

我想在python中使用请求模块执行get请求。然而,在我可以做一个让网站重定向到登录页面之前。我需要先登录,然后登录到我请求的页面。python请求重定向模块

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

  <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) 

回答

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