Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html agility pack not loading url

I have something like this:

class MyTask
{
    public MyTask(int id)
    {
        Id = id;
        IsBusy = false;
        Document = new HtmlDocument();
    }

    public HtmlDocument Document { get; set; }
    public int Id { get; set; }
    public bool IsBusy { get; set; }
}

class Program
{
    public static void Main()
    {
        var task = new MyTask(1);
        task.Document.LoadHtml("http://urltomysite");
        if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
        {
            task.IsBusy = false;
            return;
        }   
    }
}

Now when I start my program, it throws an error on the if sttement, saying that Object reference not set to an instance of an object.. Why isn't it loading my page? What am I doing wrong here?

like image 489
ojek Avatar asked Nov 05 '13 16:11

ojek


2 Answers

You are looking for .Load().

.LoadHtml() expects to be given physical HTML. You are giving a website to go to:

HtmlWeb website = new HtmlWeb();
HtmlDocument rootDocument = website.Load("http://www.example.com");
like image 149
Arran Avatar answered Nov 01 '22 20:11

Arran


In addition to Arran's answer

If .SelectNodes("//span[@class='some-class']") doesn't return any nodes and is null then doing a Count on it will give this exception.

Try

if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']") != null && 
    task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
    {
        task.IsBusy = false;
        return;
    }   
like image 1
Harrison Avatar answered Nov 01 '22 20:11

Harrison