如何在ASP.NET网站上使用python请求登录并获取数据?

时间:2019-07-26 18:52:22

标签: python python-requests

我正在尝试制作一个脚本,该脚本将登录到url,然后从中获取数据。该网页是使用ASP.NET构建的,示例登录页面如下所示。

<div id="ctl00_ContentPlaceHolderLogin_divLogin" class="NavNews" style="width: 300px; letter-spacing: normal; margin-top: 10px;
   font-size: 14px; margin-left: auto; margin-right: auto;">
   <div style="padding: 5px 5px 5px 5px;">
      <table id="ctl00_ContentPlaceHolderLogin_Login1" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
         <tr>
            <td>
               <div style="width: 125px; float: left; text-align: right; padding-top: 3px;">User ID:</div>
               <div style="float: left; padding-left: 5px">
                  <input name="ctl00$ContentPlaceHolderLogin$Login1$UserName" type="text" value="test123" id="ctl00_ContentPlaceHolderLogin_Login1_UserName" autocomplete="off" style="width:140px;"/>
               </div>
               <div style="width: 125px; float: left; text-align: right; padding-top: 3px;">Password:</div>
               <div style="float: left; padding-left: 5px; padding-top: 3px; padding-bottom: 10px;">
                  <input name="ctl00$ContentPlaceHolderLogin$Login1$Password" type="password" id="ctl00_ContentPlaceHolderLogin_Login1_Password" onkeypress="capLock(event)" autocomplete="off" style="width:140px;"/>
               </div>
               <br/>
               <input type="image" name="ctl00$ContentPlaceHolderLogin$Login1$Submit" id="ctl00_ContentPlaceHolderLogin_Login1_Submit" src="Images/submit.gif" style="border-width:0px;"/>
               <a id="ctl00_ContentPlaceHolderLogin_Login1_Clear" href="Login.aspx">
               <img src="Images/clear.gif" style="border-width:0px;"/>
               </a>
               <br/>
               <a id="ctl00_ContentPlaceHolderLogin_Login1_ForgotPWD" href="ForgotPassword.aspx" style="color:White;font-weight:bold;">Forgot Password?</a>
            </td>
         </tr>
      </table>
   </div>
</div>

所以现在我要登录,并希望登录后从页面获取数据。所以我尝试如下使用请求;

LOGIN = 'https://www.leretanet.com/login.aspx?ReturnUrl=%2fdefault.aspx'
PROTECTED_PAGE = 'https://www.leretanet.com/Tax/TaxAmountReports.aspx?Menu=C#'
#requestUrl = 'https://www.leretanet.com/Tax/TaxAmountReports.aspx?Menu=C'

payload = {
        'ctl00_ContentPlaceHolderLogin_Login1_UserName': 'test123',
        'ctl00_ContentPlaceHolderLogin_Login1_Password': 'test123',
        'ctl00$ContentPlaceHolderLogin$Login1$Submit': 'submit'
        }

with requests.Session() as s:
    s.headers['User-Agent'] = 'My user-agent'
    r = s.post(LOGIN, data=payload)
    response = s.get(PROTECTED_PAGE)
    print(response.text)

我希望登录,它会从登录信息中打印页面的文本。

但是,print(response.text)行仅打印首页(未登录)

0 个答案:

没有答案