2014-04-12 33 views
0

如何屏幕廢料由JavaScript生成的HTML頁面? 我用盡這樣的事情:如何屏幕由javascript生成的廢料html頁面

 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
     HtmlWeb hw = new HtmlWeb(); 
     doc = hw.Load("http://stats.nba.com/scores.html?gameDate=04/11/2014"); 
     HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@id='scoreboards']"); 

,因爲是由JavaScript生成內容然而,這並不工作。有沒有一種方法可以在javascript生成html之後刮掉頁面? 也許使用其他的東西然後敏捷包?

+0

用戶WebBrowser控件loded內容和元素巫婆如果u要放棄由JavaScript –

+0

@GujjuDeveloper創建的HTML,你可以給我一個例如或鏈接到我?:) –

回答

1

您可以使用一個webbrowser對象,它是一個c#對象,充當瀏覽器並運行javascript代碼,並在收到響應後使用敏捷包解析它。

MSDN - Web Browser

+0

有沒有任何如何使用它的例子? –

+0

這裏有一個基本的教程: http://www.dotnetperls.com/webbrowser –

2

使用WebBrowser控件獲得通過JS或AJAX

private void LoadHtmlWithBrowser(String url) 
{ 
    webBrowser1.ScriptErrorsSuppressed = true; 
    webBrowser1.Navigate(url); 

    waitTillLoad(this.webBrowser1); 

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
    var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)webBrowser1.Document.DomDocument; 
    StringReader sr = new StringReader(documentAsIHtmlDocument3.documentElement.outerHTML); 
    doc.Load(sr); 
} 

private void waitTillLoad(WebBrowser webBrControl) 
{ 
    WebBrowserReadyState loadStatus; 
    int waittime = 100000; 
    int counter = 0; 
    while (true) 
    { 
     loadStatus = webBrControl.ReadyState; 
     Application.DoEvents(); 
     if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive)) 
     { 
      break; 
     } 
     counter++; 
    } 

    counter = 0; 
    while (true) 
    { 
     loadStatus = webBrControl.ReadyState; 
     Application.DoEvents(); 
     if (loadStatus == WebBrowserReadyState.Complete && webBrControl.IsBusy != true) 
     { 
      break; 
     } 
     counter++; 
    } 
} 
+1

謝謝!我有一些問題,WebBrowser1在哪裏發生了變化?你是什​​麼意思由ajax或js加載? –