// This script prompts for a Symbol of a Stock and downloads Weekly // Quotes from Yahoo and Quarterly and Annual Financial Reports from // the SEC (Securtities Exchange Commision) // // Input : Company Stock Symbol // Output: Quotes.csv file in the specified path and subdirectory derived from symbol // Output: .txt files in the specified path and subdirectory derived from symbol // Output: Company Name // // Note : Currently, the directory must exist before the script is executed // For example, c:\temp\msft, if you are going to download Microsoft financial data // When prompted by Excel to 'Open' or 'Save', select Open and it will proceed to download // the quotes for the stock symbol and then download the 10-Q and 10-K reports as text files. // Uncomment code section below if you want to save as HTML file extension // Author: Curtis Kern, bkernc@yahoo.com program GetFinancialData; const // Path to the main repository of information, data will be stored in subdirectories sPath = 'C:\Temp\'; var strSymbol : string; // Stock Symbol as it appears on Yahoo strSearch : string; // Search string used to identify Company Name strName : string; // Company Name of Stock strDirectory: string; // Directory name derived from Stock Symbol strFileName : string; // File name excluding Directory Path bReturn : boolean; // Return Code from functions strLinkURL : string; // URL from which multiple links will be navigated to procedure OnDocumentComplete(URL : string); var nPos : integer; begin // Yahoo Main Page // Collect Company Symbol // Open Yahoo Weekly Quote page if IsPartOf('http://www.yahoo.com', URL) then begin // Prompt user for Company Symbol strSymbol := Readln('Enter Company Symbol: '); // Check for if Trim(strSymbol) = '' then begin GoHome; Exit; end; // Go to intermediate page to have Yahoo calculate available dates GoToURL('http://finance.yahoo.com/q/hp?s=' + strSymbol + '&g=w'); end; // Yahoo Weekly Quote Page // Parse for Company Name // Open spreadsheet if IsPartof('http://finance.yahoo.com/q/hp', URL) then begin // Derive search string for Parse strSearch := '(' + strSymbol + ')'; // Find the Company Name by Parsing the HTML // Parse (); // Click on Download to Spreadsheet link bReturn := ClickHyperLink('Download To Spreadsheet'); // Check for Invalid Symbol if not bReturn then begin ShowMessage('Invalid Company Symbol: ' + strSymbol); GoToURL ('http://www.yahoo.com'); end; end; // Yahoo Weeekly Quote Spreadsheet Page // Construct Directory Name // Save Spreadsheet // Open SEC Company Search page if IsPartOf('http://ichart.finance.yahoo.com/table.csv', URL) then begin // Get File Name strFileName := 'quotes.CSV'; // Derive Directory from Symbol // Some symbols have a suffix on them (i.e. '.PK') strDirectory := strSymbol; if Pos('.', strSymbol) > 0 then begin strDirectory := Copy(strSymbol, 1, Pos('.', strSymbol) - 1); end; // Save File DeleteFile (sPath + strDirectory + '\' + strFileName); GetURLFile (sPath + strDirectory + '\' + strFileName, URL); // Open SEC Company Search page // Backgound Excel window should close GoToURL('http://www.sec.gov/edgar/searchedgar/companysearch.html') end; // SEC Company Search page // Fill in Company Name and search for matching companies if IsPartOf('http://www.sec.gov/edgar/searchedgar/companysearch.html', URL) then begin Fill('CIK', strSymbol); ClickButton(''); end; // SEC Company Filings page or SEC Multiple Companies page // Attempt to filter just 10-Q and 10-K reports - inabilty to click means Multiple hits if IsPartOf('http://www.sec.gov/cgi-bin/browse-edgar?company=', URL) or IsPartOf('http://www.sec.gov/cgi-bin/browse-edgar?action=', URL) then begin // Filter to just 10- reports Fill('type', '10-'); ClickButtonUsingCaption('Retrieve Filings'); end; // SEC Filtered Company Filings page // Save this URL to return multiple times and process each desired link // Open SEC Report text page if IsPartOf('http://www.sec.gov/cgi-bin/browse-edgar?type=10-', URL) then begin // Save the URL that contains the report list so that we can come back to it strLinkURL := URL; // The ClickMultiHyperLink function will click the next matching link that // has not already been navigated to bReturn := ClickMultiHyperLink('[text]'); if not bReturn then begin // Script is complete ShowMessage('Download of data for Company: ' + strName + ' ' + strSearch + ' is complete!'); end; end; // SEC Report Text page // Create Filename // Save file // Return to SEC Filtered Company Filings page if IsPartOf('http://www.sec.gov/Archives/edgar/data/', URL) then begin // Get File Name strFileName := URL; while Pos('/', strFileName) > 0 do begin strFileName := Copy(strFileName, Pos('/', strFileName) + 1, 256); end; // *** change file extension to html *** {nPos := Pos('.', strFileName); if (nPos > 0) then begin strFileName := Copy(strFileName, 1, nPos-1) + '.' + 'html'; writeln(strFileName); end;} // Save File DeleteFile(sPath + strDirectory + '\' + strFileName); GetURLFile(sPath + strDirectory + '\' + strFileName, URL); WriteLn(sPath + strDirectory + '\' + strFileName); // jump back to the 10-Q list using the save URL GoToURL(strLinkURL); end; end; // Main Procedure begin // Begin at Generic location Navigate('http://www.yahoo.com'); end.