如何篩選由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後刮掉頁面?也許使用其他敏捷包裝?
使用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++;
}
}