2011-09-19 57 views
0
<table class="result" summary="Summary Description."> 
<tbody> 
<tr> 
    <th scope="col" class="firstcol">Column 1</th> 
    <th scope="col">Column 2</th> 
    <th scope="col">Column 3</th> 
    <th scope="col" class="lastcol">Column 4</th> 
</tr> 
<tr class="even"> 
    <td class="firstcol">Text 1</td> 
    <td>Text 2</td> 
    <td>4Text 3</td> 
    <td class="lastcol">Text 4</td> 
</tr> 
</tbody></table> 

HTML Im感兴趣的部分看起来像这样。我想要文本1,文本2,文本3和文本4.使用HTMLAgilityPack,我如何提取数据?我谷歌和检查这个网站,但没有找到一些完全符合我的情况。使用HTMLAgilityPack提取特定的HTML文本

 if (htmlDoc.DocumentNode != null) 
     { 
      foreach (HtmlNode text in htmlDoc.DocumentNode.SelectNodes(???) 
      { 
       ??? 
      } 
     } 

回答

1

试试这个:

 var html = @"<table class=""result"" summary=""Summary Description.""> <tbody> <tr>  <th scope=""col"" class=""firstcol"">Column 1</th>  <th scope=""col"">Column 2</th>  <th scope=""col"">Column 3</th>  <th scope=""col"" class=""lastcol"">Column 4</th> </tr> <tr class=""even"">  <td class=""firstcol"">Text 1</td>  <td>Text 2</td>  <td>4Text 3</td>  <td class=""lastcol"">Text 4</td> </tr> </tbody></table>"; 
     var doc = new HtmlDocument(); 
     doc.LoadHtml(html); 
     var textNodes = doc.DocumentNode.SelectNodes(@"//tr[@class='even']/td/text()").ToList(); 
     foreach(var textNode in textNodes) 
     { 
      Console.WriteLine(textNode.InnerText); 
     } 
+0

工程就像一个梦!非常感谢你。 – mdc

+0

后续问题。如果还有一个叫做“奇怪”的类,并且我想拥有一个列表,就像它们在源代码中一样(偶数,奇数,偶数,奇数等)。那我怎么写这个问题呢?今天,我通过交换单词“偶数”到“奇数”,将“奇数”保存到第二个列表中,然后将第二个列表添加到第一个列表中。但是他们会有错误的顺序,因为它将在开始时具有所有的“偶数”,并且最终将具有所有的“奇数”。 – mdc

+0

如果它们在我的问题中是动态的呢? http://stackoverflow.com/questions/24018750/how-to-use-htmlagilitypack-to-extract-html-data – SearchForKnowledge

相关问题