Extracting Information from Web Pages using C#

using System;

using System.Net;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //Extracting currency rate from a website

            string url = @"http://61.8.149.19/webasp/price_kNs.asp?tcd=Q31202H0";

            string startsWith = "Rs.";

            string endsWith = "</td>";

            string response = getPageSource(url);

            //Console.WriteLine(response);

            int startIdx = response.IndexOf(startsWith);

            int endIdx = response.IndexOf(endsWith, response.IndexOf(startsWith) + startsWith.Length);

            Console.WriteLine("Currency Rate = " + response.Substring(startIdx + startsWith.Length, endIdx – startIdx – startsWith.Length).Trim());

        }

        public static string getPageSource(string URL)

        {

            System.Net.WebClient webClient = new System.Net.WebClient();

            string strSource = webClient.DownloadString(URL);

            webClient.Dispose();

            return strSource;

        }

    }

}

 

Currency Rate = 48.60
Press any key to continue . . .

 

using System;

using System.Net;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int flag = 0;

            while(flag == 0)

            {

                //Extracting MSFT Stock Price from Google Finance

                string url = @"http://www.google.com/finance?q=NASDAQ:MSFT&quot;;

                string startsWith = "\"USD\",\"";

                string endsWith = "\"";

                string response = getPageSource(url);

                //Console.WriteLine(response);

                int startIdx = response.IndexOf(startsWith);

                //Console.WriteLine("Starting Index = " + startIdx);

                int endIdx = response.IndexOf(endsWith, response.IndexOf(startsWith) + 7);

                //Console.WriteLine("Ending Index = " + endIdx);

                if (startIdx > 0 && endIdx > 0 && (endIdx – startIdx) > 0)

                {

                    Console.WriteLine("MSFT Stock Price = " + response.Substring(startIdx + 7, endIdx – startIdx – 7).Trim());

                    flag = 1;

                }

            }

        }

        public static string getPageSource(string URL)

        {

            System.Net.WebClient webClient = new System.Net.WebClient();

            string strSource = webClient.DownloadString(URL);

            webClient.Dispose();

            return strSource;

        }

    }

}

MSFT Stock Price = 29.91
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s