Afnan Bashir
Afnan Bashir

Reputation: 7419

get number of pages in search results

I want to get number of pages at the bottom of any search page say google or bing then i have to store the links of those pages for further navigation.

How can this be Done?

What approach should be used HTMLAGILITYPACK or HTTPWEBREQUEST or any other.It would be great if somebody demonstrate how to get tags with specific attributes i.e class name or ID

Google has nested like href has Span inside so we can have url from href but how to get spanalt texttext so that i can save it with name as page 1 url http:/blabla.com

Upvotes: 0

Views: 672

Answers (2)

Afnan Bashir
Afnan Bashir

Reputation: 7419

Combined some code from SLaks and generated following code that gets navigation links at bottom of google

 HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
 HtmlAgilityPack.HtmlDocument doc = web.Load("http://www.google.com.pk/search?rlz=1C1SKPL_enPK414PK414&sourceid=chrome&ie=UTF-8&q=asd");

 foreach (HtmlNode table in doc.DocumentNode.Descendants("table").Where(e => e.GetAttributeValue("id", "").Contains("nav")))
 {
     foreach (HtmlNode row in table.SelectNodes("tr"))
     {
      foreach (HtmlNode cell in row.SelectNodes("th|td"))
      {
          MessageBox.Show("cell: " + cell.InnerHtml);
      }
 }

Upvotes: 1

SLaks
SLaks

Reputation: 887415

Using the HTML Agility Pack:

var doc = new HtmlWeb().Load(url);
var elem = doc.GetElementById("someID");
var classedLinks = doc.DocumentNode.Descendants("img")
    .Where(e => e.GetAttributeValue("class", "").Contains("SomeClass"));

Upvotes: 1

Related Questions