Quantcast

Get Your Google Search Position in C#

Get the WebProNews Newsletter:


[ Business]

If you want to test your position in Google for a certain search term, you can do so by using the Google website. By position I don’t mean Page Rank, but the actual place in the search results.

You can also use C# to find the position like the method shows below.

You can use the code to build a cool application that lists the positions of your website based on all your important search terms. You can change it to run asynchronously for better performance when checking multiple search terms.

/// <summary>
/// Retrives the position of the url from a search
/// on www.oogle.com using the specified search term.
/// </summary>
public static int GetPosition(Uri url, string searchTerm)
{
string raw = "http://www.google.com/search?num=39&q={0}&btnG=Search";
string search = string.Format(raw, HttpUtility.UrlEncode(searchTerm));

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
string html = reader.ReadToEnd();
return FindPosition(html, url);
}
}
}

/// <summary>
/// Examins the search result and retrieves the position.
/// </summary>
private static int FindPosition(string html, Uri url)
{
string lookup = "(<a class=l href=\")(\\w+[a-zA-Z0-9.-?=/]*)";
MatchCollection matches
= Regex.Matches(html, lookup);

for (int i = 0; i < matches.Count; i++)
{
string match = matches[i].Groups[2].Value;
if (match.Contains(url.Host))
return i + 1;
}

return 0;
}

Examples of use

You simply provide the method with your website URL and the search term to test and it returns the position.

Uri url = new Uri("http://www.example.com");
int position = GetPosition(url, "search term");

Google+Search+Position+In+C.aspx” class=”bluelink”>Comments

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

Get Your Google Search Position in C#
This entry was posted in Business.
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer
Top Rated White Papers and Resources
  • deepak

    which library should we use
    using …….

    as it show HttpWebRequest does not exist.
    i am making desktop application for search and it show this problem.
    is it because this code does not run in desktop application.

    • http://www.isd-weberfede.de Weber-Frede

      you must set the using statement.

      using System.net

      I think this is for HtmlWebRequest ;)

  • http://www.seodev.it SeoDev seo web napoli

    Hallo,
    I have issues using this code, because it always gives me position = 0…
    I am italian and I use italian Google page, https://www.google.it/
    when i write my keyword in google textbox, for example “seo”, it gives me the following querystring:
    http://www.google.it/webhp?hl=it&tab=ww#hl=it&sclient=psy-ab&q=seo&oq=seo&aq=f&aqi=g10&aql=&gs_sm=3&gs_upl=30109l1908081l1l1908432l9l9l3l0l0l0l112l573l5.1l6l0&gs_l=hp.3..0l10.30109l1908081l1l1908432l9l9l3l0l0l0l112l573l5j1l6l0.llsin.&psj=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=7cfce532619a9cc1&biw=1680&bih=878

    can anyone help me please ?
    THX

  • http://vnnol.com vietnam online

    Thank for your share. Finnaly, I found out my solution.

  • http://hdlss.com Saurabh

    This thing always gives me a result 0 . I have tried this code from other sites also but the result is the same . Kindly suggest something else .

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom