Simple program needed to read and parse information from finance.yahoo.com
-
I need a very simple programming project to be completed. Here are the requirements: The program will read a list of stock name and stock symbol from a text file (tab delimeted) and output the following data for each stock Stock Name Stock Symbol Price Sales Ratio 52 Week Low Price Current Price Appreciation of Stock Price (current price compared to 52 Week Low Price) express in % - The equation for this is (current price - 52 Week Lo)/52 Week Lo *100 The output file needs to be tab delimated Example the input file is: Walmart(TAB)WMT the output file should be Walmart(TAB)WMT(TAB)0.94(TAB)50.50(TAB)56.59(TAB)12.06 Basically this data is all there on http://finance.yahoo.com. All that is required is a program to read the data off finance.yahoo.com and parse the relevant information. For example, above information on Walmart is available at http://finance.yahoo.com/q?s=WMT and http://finance.yahoo.com/q/ks?s=WMT. Notice that WMT is stock symbol for Walmart. The program needs to run under Windows 98 & above. Elaborate interface is not required. It can be a simple command line program. I will need the source code for the program.
-
Answer:
Hello Kr This solution is crying out for a small Perl script so I have tackled it this way. If you do not have Perl installed on your system you can download it at http://www.activestate.com. You will also need to have installed the LWP and HTML::TokeParser modules. ActivePerl Download: http://www.activestate.com/Products/ActivePerl/ How to install modules: http://forums.devshed.com/archive/t-143549 The source code for the Perl script is: #======================================================== #!/usr/bin/perl # so what are we using? use strict; use warnings; use LWP; use HTML::TokeParser; # set up other variables my ($browser,$url,$stream,$tag,$response,$ttm,$low,$current,$appr,$text,$output); # initialise variables for the pretend browser and start the browser $browser = LWP::UserAgent->new(); # read in contents of file my $filename = "stocks.txt"; open( FILE, "< $filename" ) or die "Can't open $filename : $!"; while( <FILE> ) { chomp($_); my ($stockname,$code) = split("\t",$_); # where are we searching? $url = "http://localhost/" . $code; #$url = "http://finance.yahoo.com/q/ks?s=" . $code; print $code; # ok, get the page we are searching $response = $browser->get($url); # parse the page tag by tag $stream = HTML::TokeParser->new( $response->content_ref ); $stream->{'textify'} = {}; # remove [img] etc entities # current price $response->content =~ /<big><b>(.*)<\/b><\/big>/i || warn "Not matched"; $current = $1; # get the other data while ( $tag = $stream->get_tag('td') ) { $text = $stream->get_trimmed_text('/td'); if ( $text eq 'Price/Sales (ttm):' ) { $tag = $stream->get_tag('td'); $ttm = $stream->get_trimmed_text('/td'); } if ( substr($text,0,11) eq '52-Week Low' ) { $tag = $stream->get_tag('td'); $low = $stream->get_trimmed_text('/td'); } }; # end while # calculate appreciation $appr = $current-$low; $appr = $appr/$low; $appr = sprintf("%.2f",$appr*100); $output .= $stockname . "\t" . $code . "\t" . $ttm . "\t" . $low . "\t" . $current . "\t" . $appr . "\n"; } close FILE; open (FILEHANDLE, ">output.txt") or die "no such file"; print FILEHANDLE $output; close (FILEHANDLE); exit(0); #======================================================== It will read in a file called stocks.txt which contains the stocks you wish to check on separate lines in the format STOCKNAME<tab>STOCKCODE It then output a file called output.txt. Stocks.txt should be placed in the same folder as the above perl script, output.txt will also be created in this same folder. If you have any questions or queries please ask for clarification and I will do my best to help you.
kr-ga at Google Answers Visit the source
Related Q & A:
- Why is my information on my Yahoo page not current?Best solution by Yahoo! Answers
- How do I delete information on my Yahoo profile?Best solution by au.answers.yahoo.com
- How do i change my account into a yahoo.com from yahoo.com.au?Best solution by Yahoo! Answers
- How can I change some of the information for my Yahoo account?Best solution by Yahoo! Answers
- How to change @yahoo.co.uk to @yahoo.com?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.