簡體   English   中英

如何使用C#網頁抓取AJAX更新面板?

[英]How to web scrape AJAX Update Panel with c#?

我希望通過網絡抓取具有AJAX更新面板的網站。 我已經能夠使用正確構造的HTTP請求(HttpWebRequest)登錄到網站,並且能夠發送POST請求以獲取UpdatePanel的內容,但是它具有占位符文本而不是實際數據。

這是我請求獲取UpdatePanel數據的代碼:

// Already sent POST request with username and password to get session id, cookie etc
// Create POST data and convert it to a byte array. This includes viewstate, eventvalidation etc.
postData = String.Format("ctl00%24ScriptManager1=ctl00%24uxContentPlaceHolder%24Panel%7Cctl00%24uxContentPlaceHolder%24uxTimer&__EVENTTARGET=ctl00%24uxContentPlaceHolder%24uxTimer");
postData = hiddenFields.Aggregate(postData, (current, field) => current + ("&" + Uri.EscapeDataString(field.Key) + "=" + Uri.EscapeDataString(field.Value)));

byteArray = Encoding.UTF8.GetBytes(postData);

// Set the ContentType property of the WebRequest.
request.Headers.Add("X-MicrosoftAjax", "Delta=true");
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
request.Referer = "https://www.example.com/Registered/MyAcount.aspx?menu=My%20account";
request.Host = "www.example.com";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.

response = (HttpWebResponse)request.GetResponse();
_container.Add(response.Cookies);

using (var reader = new StreamReader(response.GetResponseStream()))
{
    // Read the content.
    responseFromServer = reader.ReadToEnd();
}

response.Close();

這是我得到的響應的摘要版本:

6259|updatePanel|ctl00_uxContentPlaceHolder_uxUpdatePnl|
<table cellpadding="0" cellspacing="0" border="0" width="100%" id="transtable">
    <tr>
        <td>
            <p>
                <div id="ctl00_uxContentPlaceHolder_UpdateProgress2" style="display:none;">

                    <div>
                        <img src="../Include/Images/loading.gif" alt="progressImg" />
                        <span id="ProgressMsg" style="font-size: small">Please, wait ... </span>
                    </div>

                </div>
            </p>
        </td>
    </tr>
    <tr>
        <td></td>
    </tr>
    <tr>
        <td></td>
    </tr>
</table>

這是預期的結果:

2577|updatePanel|ctl00_uxContentPlaceHolder_uxUpdatePnl|
<table cellspacing="0" border="0" id="ctl00_uxContentPlaceHolder_uxMyCards" style="width:100%;border-collapse:collapse;">
    <tr>
        <th align="left" scope="col" style="font-size:12px;font-weight:bold;height:40px;">Card number</th>
        <th align="left" scope="col" style="font-size:12px;font-weight:bold;">Account holder</th>
        <th align="left" scope="col" style="font-size:12px;font-weight:bold;">Balance money</th>
        <th align="left" scope="col" style="font-size:12px;font-weight:bold;">Type</th>
    </tr>
    <tr>
        <td valign="top" style="font-size:12px;width:110px;">
            <a id="ctl00_uxContentPlaceHolder_uxMyCards_ctl02_uxManageAccount" href="ManageMyCard.aspx?menu=Manage my card&amp;cno=GgxQxwWICtY4hnlrIZfFzdqc8KMXxVp9" style="font-size:11px;">308425020219083</a>
        </td>
        <td valign="top" style="font-size:12px;width:130px;">
            My Name
        </td>
        <td align="left" valign="top" style="font-size:12px;width:100px;">
            $1.50
        </td>
        <td valign="top" style="font-size:12px;width:110px;"></td>
    </tr>
    <tr>
        <td valign="top" style="font-size:12px;width:110px;">
            <a id="ctl00_uxContentPlaceHolder_uxMyCards_ctl03_uxManageAccount" href="ManageMyCard.aspx?menu=Manage my card&amp;cno=hkbnmVzj%2ftrs%2fVLXK0rBQhB0enOO%7b4Uf" style="font-size:11px;">308425026724813</a>
        </td>
        <td valign="top" style="font-size:12px;width:130px;">
            My Name
        </td>
        <td align="left" valign="top" style="font-size:12px;width:100px;">
            $4.04
        </td>
        <td valign="top" style="font-size:12px;width:110px;"></td>
    </tr>
</table>

它看起來是在實際加載數據之前請求頁面並發送了響應。 有什么辦法讓HttpWebRequest等待所有數據加載完畢后再發送響應?

我可以發布實際的HTTP請求(如果有幫助的話),但它看起來與瀏覽器中的請求幾乎相同。 在人們跳來跳去問之前,沒有關於我在做什么的API,也沒有以任何方式違法:)

編輯:為此,寧願堅持使用HttpWebRequest,而不是像硒這樣的第三方工具

您無法通過向頁面發出HTTP請求來執行此操作,因為您將獲得的只是服務器的HTML。 頁面中的JavaScript不會被評估,因此您不會獲得UpdatePanel的內容。 一種選擇是向處理程序發出請求,該處理程序返回UpdatePanel的內容。 第二種選擇是您可以使用諸如PhantomJS之類的無頭測試工具,該工具實際上將呈現頁面並在頁面中執行JavaScript。 UpdatePanel將更新,您將能夠獲取更新的內容。

我解決了這個問題,我在HTTP請求中兩次發送了__EVENTTARGET。 UpdatePanel現在可以正確加載所有數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM