簡體   English   中英

在C#中獲取動態網頁源

[英]Getting a dynamic web page source in C #

如何使用C#下載動態網頁源代碼? 更具體地說,例如,我有一個頁面http://example.com 正在下載源代碼,但是由於AJAX會在源代碼中添加幾行,所以在收集之后,我沒有得到想要的東西。 有誰知道如何“刷新”源代碼,或者根本沒有辦法實現這樣的目標? 您現有的“靜態”代碼:

WebClient client = new WebClient();
Byte[] pageData = client.DownloadData("http://example.com" + address);
string pageHtml = Encoding.UTF8.GetString(pageData);
Console.WriteLine(pageHtml);
Console.ReadKey();

問候。

您可以使用WebBrowser組件創建一個Form。 假設您將其命名為browser

private void PrepareDocument()
{
   browser.Navigate("http://somewebsite.com");
   var timer = new Timer(1000);
   timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
   timer.Enabled = true;
}

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
   //parse the document, find the data that should be loaded after ajax call
   if(browser.ReadyState == WebBrowserReadyState.Complete && 
      browser.Document.GetElementById("ajax-divId") != null)
   {
      timer.Enabled=false;
      ProceedOnDocument();
   }
}

private void ProceedOnDocument()
{
   //your code here
}

暫無
暫無

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

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