parse google search results

c# html-agility-pack parsing

Question

How can I parse Google results as in my example?

<div class="srg">
<li class="g">...</li>
<li class="g">...</li>
<li class="g">...</li>
<li class="g">...</li>
<li class="g">...</li>
<li class="g">...</li>
</div>

This is my code for parsing Google results, selectNodes remains null.

HtmlAgilityPack.HtmlDocument doc1 = new HtmlAgilityPack.HtmlDocument();
StreamReader reader = new StreamReader(WebRequest.Create("http://www.google.com/?gws_rd=ssl#q=(404)8271500").GetResponse().GetResponseStream(), Encoding.Default); //put your encoding            
doc1.Load(reader);

var selectNodes = doc1.DocumentNode.SelectNodes("//li[@class='g']");
foreach (var node in selectNodes)  
{
    //node.InnerText will give you the text content of the li tags ...
}

Popular Answer

Sample code:

        string result = @"<div class=""srg"">
                        <li class=""g"">...</li>
                        <li class=""g"">...</li>
                        <li class=""g"">...</li>
                        <li class=""g"">...</li>
                        <li class=""g"">...</li>
                        <li class=""g"">...</li>
                        </div>";

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(result);
        var selectNodes = doc.DocumentNode.SelectNodes("//li[@class='g']");
        foreach (var node in selectNodes)
        {
           //node.InnerText will give you the text content of the li tags ...
        } 


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why