//=========================================================================== // " WEB HARVEST " // HOW TO HARVEST DATA FROM THE WEB // Using FastNet Components // Nicholas Gessler // 1 February 2006 //=========================================================================== #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //=========================================================================== // VARIABLES //=========================================================================== // for the general routine AnsiString text; // for the special "Get Gold Price" routine: int p; AnsiString goldWebAddress; AnsiString goldText; AnsiString goldSubText; AnsiString goldPrice; //=========================================================================== // FUNCTIONS //=========================================================================== //--------------------------------------------------------------- Get Website void getWebsite (void) { Form1->NMHTTP1->Get(Form1->EditWebAddress->Text); Form1->MemoHeader->Text = Form1->NMHTTP1->Header; Form1->MemoBody->Text = Form1->NMHTTP1->Body; text = Form1->NMHTTP1->Body; } //------------------------------------------------------------ Search Website void searchWebsite (void) { Form1->EditSearchStringLength->Text = Form1->EditSearchString->Text.Length(); Form1->EditSearchStringPosition->Text = text.Pos(Form1->EditSearchString->Text); } //--------------------------------------------------------- Get Price of Gold void getGoldPrice (void) { // go to the gold website goldWebAddress = "http://cmi-gold-silver.com/gold-silver-daily-spot-prices.html"; Form1->NMHTTP1->Get(goldWebAddress); // display the URL and HTML on the screen Form1->EditWebAddress->Text = goldWebAddress; Form1->MemoHeader->Text = Form1->NMHTTP1->Header; Form1->MemoBody->Text = Form1->NMHTTP1->Body; // capture the HTML Body in AnsiStrings goldText = Form1->NMHTTP1->Body; text = Form1->NMHTTP1->Body; // it may not be easy to find what you want // here we narrow the search to two steps // STEP 1: search for a unique marker near the gold price p = goldText.Pos(">GoldEditGoldMarker->Text = ">GoldEditGoldMarkerPosition->Text = p; // create a new string from the marker to the gold price goldSubText = goldText.SubString(p, 200); Form1->MemoGoldSubText->Text = goldSubText; // STEP 2: search for the first dollar sign p = goldSubText.Pos("$"); goldPrice = goldSubText.SubString(p, 7); Form1->EditGoldPrice->Text = goldPrice; Form1->EditGoldPricePosition->Text = p; } //=========================================================================== // EVENT HANDLERS //=========================================================================== //-------------------------------------------------------- Get Website Button void __fastcall TForm1::ButtonGetWebsiteClick(TObject *Sender) { getWebsite(); } //-------------------------------------------------- Search for String Button void __fastcall TForm1::ButtonSearchForStringClick(TObject *Sender) { searchWebsite(); } //----------------------------------------------------- Get Gold Price Button void __fastcall TForm1::ButtonGetGoldPriceClick(TObject *Sender) { getGoldPrice(); } //---------------------------- On Pressing ENTER after Typing the Web Address void __fastcall TForm1::EditWebAddressKeyPress(TObject *Sender, char &Key) { if (Key == VK_RETURN) getWebsite(); } //-------------------------- On Pressing ENTER after Typing the Search String void __fastcall TForm1::EditSearchStringKeyPress(TObject *Sender, char &Key) { if (Key == VK_RETURN) searchWebsite(); } //---------------------------------------------------- Show Connection Status void __fastcall TForm1::NMHTTP1Connect(TObject *Sender) { StatusBar1->SimpleText = " Connected"; } //---------------------------------------------------- Show Connection Status void __fastcall TForm1::NMHTTP1HostResolved(TComponent *Sender) { StatusBar1->SimpleText = " Host Resolved"; } //---------------------------------------------------- Show Connection Status void __fastcall TForm1::NMHTTP1PacketRecvd(TObject *Sender) { StatusBar1->SimpleText = " " + IntToStr(NMHTTP1->BytesRecvd) + " bytes of " + IntToStr(NMHTTP1->BytesTotal) + " received"; } //---------------------------------------------------- Show Connection Status void __fastcall TForm1::NMHTTP1PacketSent(TObject *Sender) { StatusBar1->SimpleText = " " + IntToStr(NMHTTP1->BytesSent) + " bytes of " + IntToStr(NMHTTP1->BytesTotal) + " sent"; } //---------------------------------------------------- Show Connection Status void __fastcall TForm1::NMHTTP1Success(CmdType Cmd) { StatusBar1->SimpleText = " Success"; } //---------------------------------------------------------- That's All Folks