如何使用html敏捷包获得tr

时间:2016-05-12 06:44:13

标签: html parsing html-agility-pack

html就像这样

<tr>
<th class="label">Brand</th>
<td class="data">Dell</td>
</tr>
<tr>
<th class="label">Series</th>
<td class="data">Inspiron Touch</td>
</tr>
<tr>
<th class="label">Color</th>
<td class="data">Black & Red</td>
</tr>
<tr>
<th class="label">Operating System</th>
<td class="data">Windows 10 Home</td>
</tr>
<tr>
<th class="label">Screen Size</th>
<td class="data">15.6"</td>
</tr>
<tr>
<th class="label">Touchscreen</th>
<td class="data">Yes</td>
</tr>
<tr>
<th class="label">Screen Resolution</th>
<td class="data">3840 x 2160</td>
</tr>
<tr>
<th class="label">Processor Type</th>
<td class="data">Intel Core i7 6th Gen</td>
</tr>
<tr>
<th class="label">Processor Speed</th>
<td class="data">2.6 GHz</td>
</tr>
<tr>
<th class="label">Processor Turbo</th>
<td class="data">Up to 3.5 GHz</td>
</tr>
<tr>
<th class="label">Processor Cache</th>
<td class="data">6 MB</td>
</tr>
<tr>
<th class="label">Processor Model</th>
<td class="data">i7-6700HQ</td>
</tr>
<tr>
<th class="label">RAM</th>
<td class="data">16GB</td>
</tr>
<tr>
<th class="label">RAM Type</th>
<td class="data">DDR3L</td>
</tr>
<tr>
<th class="label">Hard Disk Capacity</th>
<td class="data">1TB + 128GB SSD</td>
</tr>
<tr>
<th class="label">HDD Interface</th>
<td class="data">SATA, SSD</td>
</tr>
<tr>
<th class="label">HDD RPM</th>
<td class="data">5400</td>
</tr>
<tr>
<th class="label">Graphics</th>
<td class="data">Nvidia Graphics</td>
</tr>
<tr>
<th class="label">Graphics Detail</th>
<td class="data">Nvidia GeForce GTX 960M</td>
</tr>

我想获得tr标签内每个th和td标签的标签和数据,前4个只有Brand系列颜色和操作系统,但是还有一些其他项目在tr里面也有相同的th和td类,请告诉我如何使用html敏捷包获取这些项目,我将这些4插入数据库,我使用选择节点我只得到第一个tr的内部文本,当我到达这些tr时,我正在使用此代码< / p>

 HtmlNodeCollection specsNode = null;
            specsNode = doc.DocumentNode.SelectNodes("//tr']");
            foreach (HtmlNode node in specsNode)
            {
                string htmli = node.InnerHtml;
                HtmlDocument doci = new HtmlDocument();
                doci.LoadHtml(htmli); 
    htmlNode node2 in doci.DocumentNode.SelectNodes("//td[@class='data']"){
node2.InnerText;

我现在更熟悉HAP,但tr里面的课程是相同的,我现在不知道如何获得它们。

1 个答案:

答案 0 :(得分:1)

我不知道你在这里尝试做什么,但我想.Contains函数可以帮助你解决问题,使用.Contains函数进行检查

string Result=html;
htmldocument doc=new htmldocument();
doc.Loadhtml(Result);
htmlnodecollection noden=null;
noden=doc.DocumentNode.SelectNodes("");
if(Result.Contains("your brand")==true)
{
//do something