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 = "(<h2 class=r><a 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");

A couple of months ago one of my readers asked me to build a stock quote class that would automatically update the quote. I forgot about it, but then I found the e-mail and decided to give it a go. I started looking for some free services that could provide the data in XML format or something similar that can be easy parsed. Apparently that does not exist. All the stock sites charge you for that information and I want something free, so that was not the way to go.

Then I read about the Yahoo financial website and that it could be screen scraped fairly easy, so that’s what the class will use. The class is actually very simple to both use and modify. If the class isn't exactly what you need, you can just use its screen scraping methods to create your own implementation.

Update:
I've got some comments pointing out that Yahoo can give you CSV files, so I took a look at it, and now it does not use screen scraping any more. Thanks for the comments.

Examples of use

To get the quote for a specific stock, you can call the static Execute method. In this example a text box is filled with the quote:

txtStockQuote.Text = StockEngine.Execute("MSFT").Value.ToString();

You can also hook into the automatic update event, so that you can update a text box every time an update occurs. By default it is every minute.

private StockEngine _Engine;

 

private void PrepareStockQuote()

{

  _Engine = new StockEngine("MSFT");

  _Engine.AutoUpdate = true;

  _Engine.UpdateInterval = 60000; // 1 minute

  _Engine.Updated += new EventHandler<EventArgs>(_Engine_Updated);

}

 

private void _Engine_Updated(object sender, EventArgs e)

{

  txtStockQuote.Text = _Engine.Value.ToString();

}

Download

StockEngine.zip (1,92 KB)