我一直难以找到一种使用HtmlAgilityPack解析这个html文件的有效方法。我可以阅读所有内容,但我这样做的方式非常草率,我希望得到一些建议,甚至是一些复制意大利面,如果我敢问,这样我就可以有效地阅读数据。
private void ParsePage(string html)
{
var doc = new HtmlDocument();
doc.LoadHtml(html);
if (doc.DocumentNode != null)
{
var gameTypes = doc.DocumentNode.SelectNodes("//div[@class='lifetime_stats_header']");
if (gameTypes != null) {
foreach (var gt in gameTypes)
{
if (gt != null && gt.InnerText != null && this.rtbHtmlReceived != null)
{
this.rtbHtmlReceived.AppendText("Played type : " + gt.InnerText.ToString().Replace(" ", "") + "\n");
this.rtbHtmlReceived.AppendText(
"dSibSib : " + gt.NextSibling.NextSibling.InnerText.Replace(" ", "") + "\n");
}
}
}
}
}
我正在使用我所拥有的东西打印出来,
Wins885 Kills3,805 Assists4,140 MinionKills92,536 NeutralMinionKills16,739 TurretsDestroyed741
来自一个看似
的表格 <tbody>
<tr><td class="lifetime_stats_key">Wins</td><td class="lifetime_stats_val">885</td></tr>
<tr><td class="lifetime_stats_key">Kills</td><td class="lifetime_stats_val">3,805</td></tr>
<tr><td class="lifetime_stats_key">Assists</td><td class="lifetime_stats_val">4,140</td></tr>
<tr><td class="lifetime_stats_key">Minion Kills</td><td class="lifetime_stats_val">92,536</td></tr>
<tr><td class="lifetime_stats_key">Neutral Minion Kills</td><td class="lifetime_stats_val">16,739</td></tr>
<tr><td class="lifetime_stats_key">Turrets Destroyed</td><td class="lifetime_stats_val">741</td></tr>
</tbody>
关于如何遍历lifetime_stats_val和键的任何建议或指针都会很棒!无法弄清楚:S 我会继续研究,但我会爱你,因为你可能有任何答案:P
答案 0 :(得分:0)
弄清楚了,
foreach (var gt in gameTypes.Where(gt => gt != null && gt.InnerText != null && this.rtbHtmlReceived != null))
{
var cells = gt.NextSibling.NextSibling.ChildNodes.Descendants("tr");
foreach (var item in cells)
{
rtbHtmlReceived.AppendText("Type: " + item.FirstChild.InnerText + ": ");
rtbHtmlReceived.AppendText("Value: " + item.LastChild.InnerText + "\n");
}
}