2017-11-17 112 views
2

我在網站上使用Html Agility Pack來提取一些數據。解析一些我需要的HTML很容易,但是我對這個(稍微複雜一點的)HTML片段有困難。用html敏捷包解析div中的元素[C#]

<tr> 
    <td> 
    <div onmouseover="toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class=&quot;correct_response&quot;>Obama</em><br /><br /><table width=&quot;100%&quot;><tr><td class=&quot;right&quot;>Kailyn</td></tr></table>')" onmouseout="toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')" onclick="togglestick('clue_J_1_1_stuck')"> 
... 

我需要根據clue_J_X_Y值從onmouseover div中的em類「correct_response」中獲取值。我真的不知道如何超越這個..

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//tr//td/div[@onmouseover]"); 

一些幫助,將不勝感激。

回答

1

我不知道你應該從他們身上得到什麼。但是我會給你提供你需要的所有數據。

首先我們加載HTML。

string html = "<tr>" + 
     "<td>" + 
     "<div onmouseover = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class=&quot;correct_response&quot;>Obama</em><br/><br/><table width=&quot;100%&quot;><tr><td class=&quot;right&quot;>Kailyn</td></tr></table>')\" onmouseout = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')\" onclick = \"togglestick('clue_J_1_1_stuck')\"></div></td></tr>"; 
    HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
    doc.LoadHtml(html); 
    //Console.WriteLine(doc.DocumentNode.OuterHtml); 

然後我們得到屬性值onmouseover。

 string toggle = doc.DocumentNode.SelectSingleNode("//tr//td/div[@onmouseover]").GetAttributeValue("onmouseover", "FAILED"); 

如果未能找到名爲「onmouseover」的屬性,它將返回FAILED。現在我們得到toggle方法的參數,每個參數都被兩個'(撇號)括起來。

//Get Variables from toggle() 
List<string> toggleVariables = new List<string>(); 
bool flag = false; string temp = ""; 
for(int i=0; i<toggle.Length; i++) 
{ 
    if (toggle[i] == '\'' && flag== true) 
    { 
     toggleVariables.Add(temp); 
     temp = ""; 
     flag = false; 
    } 
    else if (flag) 
    { 
     temp += toggle[i]; 
    } 
    else if (toggle[i] == '\'') 
    { 
     flag = true; 
    } 
} 

之後,我們有一個列表與3個實體。在這種情況下,它將包含以下內容。

  • clue_J_1_1
  • clue_J_1_1_stuck
  • < EM類= " correct_response " >奧巴馬</EM > < BR/> < BR/> <表寬度= " 100%" > <TR> < TD類= " right " > Kailyn </td > </tr > < /表>;

現在我們可以用第三個參數的HTML代碼創建一個新的HtmlDocument。但首先我們必須將它轉換爲可用的HTML,因爲第三個參數包含HTML中的轉義字符。

 //Make it into workable HTML 
     toggleVariables[2] = HttpUtility.HtmlDecode(toggleVariables[2]); 

     //New HtmlDocument 
     HtmlDocument htmlInsideToggle = new HtmlDocument(); 
     htmlInsideToggle.LoadHtml(toggleVariables[2]); 

     Console.WriteLine(htmlInsideToggle.DocumentNode.OuterHtml); 

並完成。其中的代碼全部在這裏。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Net; 
using HtmlAgilityPack; 
using System.Web; 

namespace test 
{ 
    class Program 
    { 

    public static void Main(string[] args) 
    { 
      string html = "<tr>" + 
       "<td>" + 
       "<div onmouseover = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class=&quot;correct_response&quot;>Obama</em><br/><br/><table width=&quot;100%&quot;><tr><td class=&quot;right&quot;>Kailyn</td></tr></table>')\" onmouseout = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')\" onclick = \"togglestick('clue_J_1_1_stuck')\"></div></td></tr>"; 
      HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
      doc.LoadHtml(html); 
      //Console.WriteLine(doc.DocumentNode.OuterHtml); 

      string toggle = doc.DocumentNode.SelectSingleNode("//tr//td/div[@onmouseover]").GetAttributeValue("onmouseover", "FAILED"); 
      //Clean up string 

      //Console.WriteLine(toggle); 

      //Get Variables from toggle() 
      List<string> toggleVariables = new List<string>(); 
      bool flag = false; string temp = ""; 
      for(int i=0; i<toggle.Length; i++) 
      { 
       if (toggle[i] == '\'' && flag== true) 
       { 
        toggleVariables.Add(temp); 
        temp = ""; 
        flag = false; 
       } 
       else if (flag) 
       { 
        temp += toggle[i]; 
       } 
       else if (toggle[i] == '\'') 
       { 
        flag = true; 
       } 
      } 

      //Make it into workable HTML 
      toggleVariables[2] = HttpUtility.HtmlDecode(toggleVariables[2]); 
      //New HtmlDocument 
      HtmlDocument htmlInsideToggle = new HtmlDocument(); 
      htmlInsideToggle.LoadHtml(toggleVariables[2]); 

      Console.WriteLine(htmlInsideToggle.DocumentNode.OuterHtml); 

      //You're on your own from here     

      Console.ReadKey(); 

    } 
}