帮助C#中的HtmlAgilityPack [英] Help with the HtmlAgilityPack in C#

查看:59
本文介绍了帮助C#中的HtmlAgilityPack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我相信我在考虑解决问题的方法。我正在尝试获取名为ConMet,Description和Type的第一个和最后两个数据值,并且还获取值的第一个td,例如100117以及最后两个td信息。诸如STUD WHEEL LEFT-HANDED 3/4X 4.84和车轮螺栓等。我想遍历整个tbody元素来获取这些值。



Hello I believe I am over thinking the solution to my problem. I am trying to grab the first and last two th data values called "ConMet", "Description", and "Type" and also grab the first td of the value such as "100117" as well as the last two td information in the tr such as STUD WHEEL LEFT-HANDED 3/4" X 4.84" and Wheel Stud. I would like to loop through the whole tbody element to grab these values.

<table>
 <tbody>
 <tr>
 <th>ConMet</th>
 <th>Customer</th>
 <th>City</th>
 <th>Religion</th>
 <th>Country</th>
 <th>Description</th>
 <th>Type</th>
 </tr>
 <tr>
 <td> <a href="LookUpHub.aspx?Part=100117"><b><b><b>1001</b></b></b>17</a></td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td>&nbsp;</td>
 <td>STUD WHEEL LEFT-HANDED  3/4" X 4.84"</td>
 <td>Wheel Stud</td>
 </tr>
 <tr>
<td><a href="LookUpHub.aspx?Part=100118"><b><b><b>1001</b></b></b>18</a></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>STUD WHEEL RIGHT-HANDED 3/4" X 4.84"</td>
<td>Wheel Stud</td>
</tr>
<tr>......













var html = new HtmlDocument();
          html.LoadHtml(new WebClient().DownloadString("https://vdm.conmet.com/hubcatalog/LookUpHub.aspx"));

          var root = html.DocumentNode;
          var table = root.Descendants()
                       .Where(n => n.Equals("tbody"))
                       .Single()
                       .Descendants("tr")





我不确定我是否在正确的轨道上感激任何帮助。



I am not sure if I am on the right track grateful for any assistance.

推荐答案

您提供的HTML似乎与从该aspx页面返回的内容不同,但由于您的代码使用了tbody我认为提供的HTML是正确的版本。



根据这个假设,我稍微调整了你的代码,然后以下版本首先得到标题值,然后遍历其余行以获得实际数据:



The HTML you provided doesn't appear to be the same as what's returning from that aspx page but since your code is using tbody" I assumed the HTML provided is the correct version.

Under that assumption I tweaked your code a little bit and the following version first gets the header values then loops through the remaining rows to get the actual data:

var root = html.DocumentNode;
var allRows = root.Descendants("tbody").Single().Descendants("tr");
var headers = allRows.ElementAt(0).Descendants("th");

var header1 = headers.ElementAt(0).InnerText;
var header2 = headers.ElementAt(1).InnerText;
var header3 = headers.ElementAt(6).InnerText;

for (int i = 1; i < allRows.Count(); i++)
{
    var currentRow = allRows.ElementAt(i);
    var firstAValue = currentRow.Descendants("td")
        .ElementAt(0)
        .Descendants("a")
        .First()
        .Attributes["href"].Value;
    // TODO: Extract "100117" from "LookUpHub.aspx?Part=100117"

    // Grab the rest of the values here...
}





希望这有帮助。



Hope this helps.


这篇关于帮助C#中的HtmlAgilityPack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆