我正在尋找網站刮一個網站,有一個AJAX更新面板。我已經能夠使用正確構造的HTTP請求(HttpWebRequest)登錄到網站,並且我可以發送POST請求來獲取UpdatePanel的內容,但它包含佔位符文本而不是實際數據。如何使用c#抓取AJAX更新面板?
這裏就是我提出請求,以獲得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&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&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,而不是使用第三方工具硒
但JavaScript必須觸發一個HTTP請求才能獲取數據。這是我上面模擬的HTTP請求。在Fiddler中,瀏覽器發出HTTP請求,並在UpdatePanel中檢索內容。當我在代碼中完成相同的HTTP請求時,我沒有收到任何東西...... – ljenkins