2011-02-25 66 views
1

我需要在執行本文檔中的JavaScript代碼之後解析html代碼。 我使用webBrowser控件下載和控制html。C#使用javascript解析html

例如,我在我的html代碼中有一些javascript。

<script type="text/javascript" src="http://site.com/script.js"></script> 

感謝您的回答。

P.S. 我的意思是:我必須用一些文本解析所有的代碼才能返回javascript。所以,我只能在執行javascript後解析文檔。如果我需要某些部分的動態內容,這些內容將與javascript一起添加。

新增

我用JavaScript生成的內容的內容。我跳過這一個,因爲我一直在尋找一些內容是它和JavaScript生成的iframe中。

現在我還有一個問題。在我的文檔中,我有幾個iframe。我試圖從一些框架中獲取內容。在接下來的方式:

 var htmlcol = webBrowser1.Document.Window.Frames; 
     foreach (HtmlWindow item in htmlcol) 
     { 
      try 
      { 
       Console.Write(item.Name); 
      } 
      catch (System.Exception ex) 
      { 
       MessageBox.Show("Something wrong"); 
      } 

     } 

但這種方式我有例外: 'System.UnauthorizedAccessException的'。我怎樣才能訪問框架的HTML?

P.P.S.索裏對我的英語不好:)

+0

你能具體談談嗎? – TheBoyan 2011-02-25 18:41:22

+1

你究竟在求什麼人幫你做?閱讀html?解析

1

我認爲你會使用使用WebBrowserDocument屬性表示DOM爲有更好的體驗。

您可以遍歷Body的嵌套元素,也可以使用GetElementByIdGetElementsByTagName找到您想要的值。

DOM應該通過JavaScript在頁面中所做的更改自動更新。

+0

謝謝。有用。也許並已經工作過。我跳過了這個,因爲我正在尋找一些在iframe中顯示的內容。這是在JavaScript中生成的。 – lc0 2011-02-25 21:35:50

0

請爲您的問題閱讀Phantomjs,並使用setTimeOut打開頁面。

這可以魔神像這樣:

var page = require('webpage').create(); 

page.open("https://sample.com", function(){ 
    page.evaluate(function(){ 
     // Execution somethings before page load. for Example: 
     localStorage.setItem("something", "whatever");// Set LocalStorage for browser before open 
    }); 

    page.open("https://sample.com", function(){ 
     setTimeout(function(){ 
      console.log(page.content); //page source 

      // Where you want to save it  
      page.render("screenshoot.png") 

      // You can access its content using jQuery 
      var fbcomments = page.evaluate(function(){ 
       return $("body").contents().find(".content") 
      }) 

      phantom.exit(); 
     },10000) 
    });  
});