Search code examples
c#html-agility-pack

scraping 3rd node using htmlagilitypack


In a webpage there are several nodes having class='inner'. But i need to the 3rd node having class='inner'. If i use

string x = textBox1.Text;
string q = "";

HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load("myweb_link" + x);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='inner']");


if (nodes != null)
{

    foreach (HtmlNode n in nodes)
    {
        q = n.InnerText;
        q = System.Net.WebUtility.HtmlDecode(q);
        q = q.Trim();
        MessageBox.Show(q);
    }

}
else
    MessageBox.Show("nothing found ");

it gives me all the nodes having class='inner'. i also know that.

But i want only the 3rd node. How can i get that???


Solution

  • Get the third node from the nodes variable using the indexer:

    var thirdNode = nodes[2];