问题描述
您好我相信我在考虑解决问题的方法。我正在尝试获取名为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> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </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> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </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.
推荐答案
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!