iEntry 10th Anniversary RSS Newsletter Advertising
Join the WebProWorld Forum!
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook
Friday, February 2, 2007

Get Your Google Search Position in C#

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");

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/

About the author:
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/

Publish A Comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
7 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
SEARCH
Popular WPN Business Resources












Subscribe to WebProNews


Send me relevant info