Return null in HtmlAgilityPack

c# html-agility-pack

Question

I want to use this code, but all of the arguments return NULL! Would you kindly explain the issue or how to repair it?

please click for a photo that captured the event.

code:

    HtmlWeb hw = new HtmlWeb();

    private void button1_Click(object sender, EventArgs e)
        {
            Crawler("http://www.avval.ir/directory", 0);
        }

    public void Crawler(string url, int pid)
        {


            try
            {

                HtmlAgilityPack.HtmlDocument doc = hw.Load("http://www.avval.ir/directory");
                HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[@class='metatag-topi metatag-keywords']");
                HtmlNodeCollection nodes2 = doc.DocumentNode.SelectNodes("//div[@class='pagination']/a[@href]");
                HtmlNodeCollection phone = doc.DocumentNode.SelectNodes("//p[@class='phone fRight ml10']");

            }
        }
1
1
10/19/2013 1:19:24 PM

Accepted Answer

Man, it seems that your xpath selection has a mistake.

HtmlNodeCollection nodes = 
    doc.DocumentNode.SelectNodes("//a[@class='metatag-topi metatag-keywords']");

Here, you employ//a[@class='metatag-topi metatag-keywords'] phrase that encompasses everything of<a> tags alongclass component valuemetatag-topi metatag-keywords . However, the page to which you are attempting to apply your expression doesn't include any such components but does have some<a class="metatag-topic metatag-keywords"> (missing c word with a letter at the endtopic ).

Just substitute: for that line.

HtmlNodeCollection nodes = 
    doc.DocumentNode.SelectNodes("//a[@class='metatag-topic metatag-keywords']");

and it must function.

0
10/19/2013 5:13:06 PM

Popular Answer

The DocumentNode.SelectNodes If no nodes are identified, the function can return null.

You may look at this relevant Why DocumentNode.SelectNodes produces an empty list.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow