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; }更多推荐
发布评论