Subject: | OPTIMIZATION - No need to parse HTML when querying Yahoo |
If you request from the <http://finance.yahoo.com/d/quotes.csv> URL, then you only need to parse a CSV file rather than an HTML file. This is a lot easier, since you just need to remove quotes and split on commas.
An example is attached.
# Script to fetch current exchange rate from Yahoo
# $Id: xr.pl,v 1.1.1.1 2005/03/02 22:57:24 Rob Exp $
use strict;
use warnings;
use LWP::Simple;
my $Args = {
from => 'GBP',
to => 'USD',
amt => 1,
debug => 0,
};
sub strip_quotes {
my $x = shift;
$x =~ s/^\"?(.+)\"$/$1/;
return $x;
}
my $Url = "http://finance.yahoo.com/d/quotes.csv?s=$Args->{from}$Args->{to}=X&f=sl1d1t1ba&e=.csv";
my $Data = get $Url;
print $Data, if $Args->{debug};
my ($Symbol, $Rate, $Date, $Time, $Bid, $Ask) = split /,/, $Data;
$Args->{date} = strip_quotes($Date);
$Args->{time} = strip_quotes($Time);
$Args->{rate} = $Rate;
$Args->{value} = $Args->{rate} * $Args->{amt};
print sprintf("%1.2f %s = %1.2f %s (as of %s %s)\n", map { $Args->{$_} }
(qw( amt from value to date time)));