HtmlAgilityPack:有没有更好的方法来写这个

时间:2018-05-02 21:40:29

标签: c# html-agility-pack

我最近开始尝试使用HtmlAgilityPack,以下代码适用于我想要实现的目标。

但是因为我刚开始探索这个问题,我想知道是否有更好的方法然后使用这么多 PreviousSibling 标签

var Application_Html = @"https://wiki.profittrailer.com/doku.php?id=application.properties";
        HtmlWeb Internet_Load_Connection = new HtmlWeb();
        var Application_Html_Loaded = Internet_Load_Connection.Load(Application_Html);
        var Wiki_Application_Part_Loaded = Application_Html_Loaded.DocumentNode.SelectSingleNode("//div[@class ='dw-content']");

        var divs = Application_Html_Loaded.DocumentNode.Descendants("div").Where(node => node.GetAttributeValue("class", "").Equals("level5")).Where(node => node.ParentNode.GetAttributeValue("class", "").Equals("dw-content")).ToList();

        foreach (var s in divs)
        {
            if (s.InnerText.IndexOf(" ") > 0)
            {
                if (s.PreviousSibling.PreviousSibling.Id.Contains("default_api") || s.PreviousSibling.PreviousSibling.Id.Contains("trading_api"))
                {
                    textBox1.AppendText(s.PreviousSibling.PreviousSibling.PreviousSibling.PreviousSibling.PreviousSibling.PreviousSibling.Id + "\n");
                    textBox1.AppendText(s.PreviousSibling.PreviousSibling.PreviousSibling.PreviousSibling.PreviousSibling.PreviousSibling.InnerText + "\n");
                    textBox1.AppendText(s.ChildNodes[3].InnerText + "\n");
                    textBox1.AppendText("\n");

                    textBox1.AppendText(s.PreviousSibling.PreviousSibling.Id + "\n");
                    textBox1.AppendText(s.PreviousSibling.PreviousSibling.InnerText + "\n");
                    textBox1.AppendText(s.ChildNodes[3].InnerText + "\n");
                    textBox1.AppendText("\n");                       
                }
                else
                {
                    textBox1.AppendText(s.PreviousSibling.PreviousSibling.Id + "\n");
                    textBox1.AppendText(s.PreviousSibling.PreviousSibling.InnerText + "\n");
                    textBox1.AppendText(s.ChildNodes[3].InnerText + "\n");
                    textBox1.AppendText("\n");
                }   
            }

        }

这背后的想法是刮擦维基以查看是否有任何更改,如果是,更新/通知我是否需要更改。

示例html作为参考资料

<h5 id="tradingexchange">trading.exchange</h5>
<div class="level5">
<pre class="code file java">trading.<span class="me1">exchange</span> <span 
class="sy0">=</span> BITTREX</pre>

<p>
Use to set the exchange you want the bot to connect to. Possible values 
(POLONIEX, BITTREX, BINANCE).
Must be in CAPITALS.
</p>
<hr />

</div>

所有内容都在 div class =“dw-content”中,我先提取

需要

h5 id 来检查是否添加了新选项

h5 innertext 需要检查格式化

p innertext 检查说明是否更新

0 个答案:

没有答案