Csharp Ninja

Csharp Ninja

  • NA
  • 97
  • 179k

Get results count from google with HttpWebRequest

Apr 21 2012 11:30 AM
I can't get results count from google with webrequest class....

here is a google search:

https://www.google.com/#hl=en&safe=off&sclient=psy-ab&q=csharp&oq=csharp&aq=f&aqi=g-s1g3&aql=&gs_nf=1&gs_l=hp.3..0i10j0l3.630367.631926.1.632221.6.6.0.0.0.0.377.1510.2-5j1.6.0.bQ6f0KVqlJo&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=658b538c23bb4e18

and result count of the search:
11,300,000

I wrote:

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/#hl=en&safe=off&output=search&sclient=psy-ab&q=yo&oq=yo&aq=f&aqi=g4&aql=&gs_nf=1&gs_l=hp.3..0l4.966.1226.0.1551.2.2.0.0.0.0.157.313.0j2.2.0.FSg1sDuz3RY&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=658b538c23bb4e18");
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                StreamReader s = new StreamReader(resp.GetResponseStream(), Encoding.ASCII);
                string result = s.ReadToEnd();
                int one = result.IndexOf("<span id=\"ab_ps_r\" style=\"width:447px\">");
                int two = result.Substring(one).IndexOf("</span>");
                textBox1.Text = result.Substring(one, two);

but it doesn't work...

BUT I can get title text from google with this code:


                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/#hl=en&safe=off&output=search&sclient=psy-ab&q=yo&oq=yo&aq=f&aqi=g4&aql=&gs_nf=1&gs_l=hp.3..0l4.966.1226.0.1551.2.2.0.0.0.0.157.313.0j2.2.0.FSg1sDuz3RY&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=658b538c23bb4e18");
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                StreamReader s = new StreamReader(resp.GetResponseStream(), Encoding.ASCII);
                string result = s.ReadToEnd();
                int one = result.IndexOf("<title>") + 7;
                int two = result.Substring(one).IndexOf("</title>");
                textBox1.Text = result.Substring(one, two);

why I can't get results count.... help me please

Answers (2)