我有这些<div></div>
结构。
<div class="RecentTrades">
<div class="TradesRow TradesHeader">
<div class="TradesDate">Date</div>
<div class="TradesTime">Time</div>
<div class="TradesPrice">Trade Prc</div>
<div class="TradesVolume">Volume</div>
<div class="TradesBuySell">Buy/Sell</div>
<div class="TradesBid">Bid</div>
<div class="TradesAsk">Ask</div>
<div class="TradesValue">Value</div>
</div>
<div class="TradesRow TradesBG_G" style="color:#ff0000;">
<div class="TradesDate">08-Jan-14</div>
<div class="TradesTime">17:05:00</div>
<div class="TradesPrice">83.80</div>
<div class="TradesVolume">1,970,861</div>
<div class="TradesBuySell">Sell* </div>
<div class="TradesBid">83.85</div>
<div class="TradesAsk">83.88</div>
<div class="TradesValue">1.652<strong>M</strong></div>
</div>
<div class="TradesRow TradesBG_W" style="color:#ff0000;">
<div class="TradesDate">07-Jan-14</div>
<div class="TradesTime">16:30:11</div>
<div class="TradesPrice">80.739</div>
<div class="TradesVolume">40,000</div>
<div class="TradesBuySell">Sell* </div>
<div class="TradesBid">83.85</div>
<div class="TradesAsk">83.88</div>
<div class="TradesValue">32.30<strong>k</strong></div>
</div>
</div>
这是我使用HTMLAgilityPack解析/删除的代码。
private void btn_fetchTrades_Click(object sender, EventArgs e)
{
try
{
var fileName = @"..\..\bin\Debug\ModifiedPages\recent2.htm";
// Load HTML
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(fileName);
HtmlNode node = doc.DocumentNode.SelectSingleNode("div[@class='RecentTrades']");
using (FileStream fs = new FileStream(@"..\..\bin\Debug\ModifiedPages\" + "Trades.txt", FileMode.Append))
using (StreamWriter sw = new StreamWriter(fs))
{
// Iterate all rows in the relevant table
HtmlNodeCollection rows = node.SelectNodes(".//div");
for (int i = 0; i < rows.Count; ++i)
{
// Iterate all columns in this row
HtmlNodeCollection cols = rows[i].SelectNodes(".//div[@class='TradesDate']");
HtmlNodeCollection cols2 = rows[i].SelectNodes(".//div[@class='TradesTime']");
HtmlNodeCollection cols3 = rows[i].SelectNodes(".//div[@class='TradesPrice']");
HtmlNodeCollection cols4 = rows[i].SelectNodes(".//div[@class='TradesVolume']");
HtmlNodeCollection cols5 = rows[i].SelectNodes(".//div[@class='TradesBuy']");
HtmlNodeCollection cols6 = rows[i].SelectNodes(".//div[@class='TradesSell']");
HtmlNodeCollection cols7 = rows[i].SelectNodes(".//div[@class='TradesBid']");
HtmlNodeCollection cols8 = rows[i].SelectNodes(".//div[@class='TradesAsk']");
HtmlNodeCollection cols9 = rows[i].SelectNodes(".//div[@class='TradesValue']");
for (int j = 0; j < cols.Count; ++j)
// Get the value of the column and print it
sw.WriteLine(cols[j].InnerText + "," + cols2[j].InnerText + "," + cols3[j].InnerText + "," +
cols4[j].InnerText.Replace(",", "") + "," + cols5[j].InnerText + "," + cols6[j].InnerText + "," + cols7[j].InnerText +
"," + cols8[j].InnerText + "," + cols9[j].InnerText + "," + "922");
}
sw.Flush();
sw.Close();
fs.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但我一直得到“对象引用未设置为对象的实例”。我必须对节点做错事,但经过这么多个小时后我无法理解。如果你们中的任何人都可以调查它会很棒。非常感谢你!
答案 0 :(得分:3)
您是否知道在您的HTML中,您有一个“BuySell”类的节点,但您在代码中单独寻找买入和卖出?那样做会。请使用您的调试器。