Html Agility Pack c#(Html Agility Pack c#)

编程入门 行业动态 更新时间:2024-10-28 22:27:49
Html Agility Pack c#(Html Agility Pack c#)

我想从每个产品中提取库存中的物品数量(td5),并且如果库存为空则存在系统在表中写入0的问题。 提取号码的我的代码是:

HtmlNode node = doc.DocumentNode.SelectSingleNode("//tr[1]//td[5]");

有了这个我当然得到了库存产品2,任何1都有一个技巧来预处理表并用0或其他方式填充td以获得正确的数字到正确的产品?

HTML:

<tr> <td class="data">product1</td> <td class="data" width="350">TEST1</td> <td class="data">27</td> <td class="data">ST</td> <td class="data"></td> <!-- <td class=data>`LIST-KWERT[j].value`</td> --> <td class="data"></td> <!-- <td class=data>`LIST-SEINM[j].value`</td> --> <!-- <td class=data>`LIST-SSPEM[j].value`</td> --> <td class="data"></td> <td class="data"></td> <td class="data"></td> <td class="data">999,9</td> </tr> <tr> <td class="data">product2</td> <td class="data" width="350">TEST2</td> <td class="data">13</td> <td class="data">ST</td> <td class="data">21,000</td> <!-- <td class=data>`LIST-KWERT[j].value`</td> --> <td class="data"></td> <!-- <td class=data>`LIST-SEINM[j].value`</td> --> <!-- <td class=data>`LIST-SSPEM[j].value`</td> --> <td class="data">5,000</td> <td class="data">10,000</td> <td class="data">243</td> <td class="data">35,5</td> </tr>

I want to extract the number of items in stock (td5) from every product, and having a problem that the system isnt writing 0 in the table if stock is empty. My Code to extract the number would be:

HtmlNode node = doc.DocumentNode.SelectSingleNode("//tr[1]//td[5]");

With this I get product2 items in stock of course, any1 got a trick for me to preprocess the table and fill the td´s with 0 or another way to grab the right numbers to the right product?

HTML:

<tr> <td class="data">product1</td> <td class="data" width="350">TEST1</td> <td class="data">27</td> <td class="data">ST</td> <td class="data"></td> <!-- <td class=data>`LIST-KWERT[j].value`</td> --> <td class="data"></td> <!-- <td class=data>`LIST-SEINM[j].value`</td> --> <!-- <td class=data>`LIST-SSPEM[j].value`</td> --> <td class="data"></td> <td class="data"></td> <td class="data"></td> <td class="data">999,9</td> </tr> <tr> <td class="data">product2</td> <td class="data" width="350">TEST2</td> <td class="data">13</td> <td class="data">ST</td> <td class="data">21,000</td> <!-- <td class=data>`LIST-KWERT[j].value`</td> --> <td class="data"></td> <!-- <td class=data>`LIST-SEINM[j].value`</td> --> <!-- <td class=data>`LIST-SSPEM[j].value`</td> --> <td class="data">5,000</td> <td class="data">10,000</td> <td class="data">243</td> <td class="data">35,5</td> </tr>

最满意答案

如果您想在每行中获得第5个td,请使用以下代码:

var html = new HtmlDocument(); html.LoadHtml(input); var root = html.DocumentNode; var list = new List<Data>(); foreach (var node in root.SelectNodes("//tr//td[5]")) { var price = node.InnerText.IsNullOrWhiteSpace() ? "no price" : node.InnerText; }

If you want to get 5th td in each row you use this code:

var html = new HtmlDocument(); html.LoadHtml(input); var root = html.DocumentNode; var list = new List<Data>(); foreach (var node in root.SelectNodes("//tr//td[5]")) { var price = node.InnerText.IsNullOrWhiteSpace() ? "no price" : node.InnerText; }

更多推荐

本文发布于:2023-07-05 22:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1042360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Html   Agility   Pack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!