本文介绍了C#HTML敏捷包解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以我一直在旋转我几个小时试图抓住这个但我根本就不能,我想知道这里是否有人可以帮助我。



我正在尝试使用HTML Agility Pack从表中解析一个tr。这是完整的表格HTML代码。



Okay so I've been spinning me head for hours and hours trying to get the hang of this but I just simply can't and I'm wondering if anyone here could help me out.

I'm trying to use HTML Agility Pack to parse only one tr from a table. Here is the full table html code.

<table border="0" cellpadding="3" cellspacing="2" style="border-collapse: collapse" width="100%">
		<tbody><tr>
			<td width="50%" style="border-removed solid; border-top-width: 2"></td>
			<td width="50%" style="border-removed solid; border-top-width: 2"></td>
		</tr>

				<tr valign="top">
			<td>Country</td>
			<td>United Kingdom</td>
		</tr>

				<tr valign="top">
			<td>Contact detail</td>
			<td>08700746464<br>Vodafone Head Office<br>The Courtyard<br>2-4 London Road<br>Newbury<br>Berkshire<br>RG14 1JX</td>
		</tr>

				<tr valign="top">
			<td colspan="2" style="height: 0.5em"></td>
		</tr>

		<tr valign="top">
			<td style="border-bottom-style: solid; border-bottom-width: 1">Ofcom Data</td>
			<td></td>
		</tr>

		<tr valign="top">
			<td>Network</td>
			<td>Vodafone Uk Ltd</td>
		</tr>



				<tr>
			<td>Change date</td>
			<td>11-2012</td>
		</tr>




	</tbody></table>





但是我想要解析的唯一部分是这个





But the actual only part I'm wanting to parse is this

Vodafone Uk Ltd





这让我疯狂,因为我之前没有使用过HTML Agility Pack,而且我有点新鲜,并希望学习。 />


只是想知道是否有人可以善待并帮助我。我知道我甚至可能都在尝试这一切都错了但是我已经尝试了很长时间并在Google上使用了许多不同的方法。



我尝试过:





This has been driving me crazy because I've not used HTML Agility Pack before and I'm kind of new and looking to learn.

Just wondered if anyone could be kind and give me a hand here. I know I'm probably even trying it all wrong BUT I have tried and for many hours with many different methods on google.

What I have tried:

HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("URL");

// Targets a specific node
HtmlNode content_wrapper = document.GetElementbyId("table");

System.Console.WriteLine(table.ToString());
HtmlNodeCollection search_results = content_wrapper.SelectNodes("//tr[@class='']");
foreach (HtmlNode result in tr)
{
    string recipe_name = result.SelectSingleNode("*[@class='Network']").InnerText;// error appears here
    System.Console.WriteLine(Network);
}
System.Console.ReadKey();

推荐答案

var tds = document.DocumentNode.SelectNodes("//table //tr//td");
           for (int i = 0; i < tds.Count; i++)
           {
               string name = tds[i].InnerText.Trim();
               if (name == "Network")
               {
                   string value = tds[i + 1].InnerText.Trim();
                  // MessageBox.Show(value);
                    Console.WriteLine(value);
                   break;
               }
           }


这篇关于C#HTML敏捷包解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:40