0

I am trying to get the value of the "Pool Hashrate" using the HTML Agility Pack. Right when I hit my string hash, I get Object reference not set to an instance of an object. Can somebody tell me what I am doing wrong?

string url = http://p2pool.org/ltcstats.php?address

protected void Page_Load(string address)
{
    string url = address;
    HtmlWeb web = new HtmlWeb();
    HtmlDocument doc = web.Load(Url);

    string hash = doc.DocumentNode.SelectNodes("/html/body/div/center/div/table/tbody/tr[1]")[0].InnerText;
}
Sae1962
  • 1,122
  • 15
  • 31
Robert
  • 646
  • 4
  • 12
  • 37
  • I believe Html Agility Pack is 1 based. I would break it down and leave off the InnerText, then do a foreach on your nodes and debug to see if you got any nodes and what is in them. – Harrison Dec 17 '13 at 02:48
  • I had a similar problem, check this out(http://stackoverflow.com/questions/30805833/inspect-element-from-my-wpf-webbrowser-using-inspect-elementsie-chrome-fir) – user254197 Jun 18 '15 at 18:22

2 Answers2

2

Assuming you're trying to access that url, of course it should fail. That url doesn't return a full document, but just a fragment of html. There is no html tag, there is no body tag, just the div. Your xpath query returns nothing and thus the null reference exception. You need to query the right thing.

When I access that url, it returns this:

<div>
    <center>
        <div style="margin-right: 20px;">
        <h3>Personal LTC Stats</h3>
        <table class='zebra-striped'>
        <tr><td>Pool Hashrate: </td><td>66.896 Mh/s</td></tr>
        <tr><td>Your Hashrate: </td><td>0 Mh/s</td></tr>  
        <tr><td>Estimated Payout: </td><td> LTC</td></tr>
        </table>
        </div>
    </center>
</div>

Given this, if you wanted to get the Pool Hashrate, you'd use a query more like this:

/div/center/div/table/tr[1]/td[2]

In the end you need to do this:

var url = "http://p2pool.org/ltcstats.php?address";
var web = new HtmlWeb();
var doc = web.Load(url);
var xpath = "/div/center/div/table/tr[1]/td[2]";
var poolHashrate = doc.DocumentNode.SelectSingleNode(xpath);
if (poolHashrate != null)
{
    var hash = poolHashrate.InnerText;
    // do stuff with hash
}
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
1

The problem is that xpath is not finding the specified node. You can specify an id to the table or the tr in order to have a smaller xpath

Also, based on your code I assume that you're looking for a single node only, so you may want to use something like this

doc.DocumentNode.SelectSingleNode("xpath");

Another good option is using Fizzler

pollirrata
  • 5,188
  • 2
  • 32
  • 50