Yahoo Finance Economic Events to XML with Perl

This is a very simple script which gets yahoo economic events from the yahoo finance website and converts it to XML.


#!c:\perl\bin\perl.exe

use LWP::Simple;
use LWP::UserAgent;
use HTML::TableExtract;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = new CGI;

my $url = “http://biz.yahoo.com/c/e.html”;
my $capture = get($url);
#http://www.treasurydirect.gov/xml/PendingAuctions.xml
print $cgi->header(-type => ‘application/xml’); # make browser expect xml

$te = HTML::TableExtract->new( depth => 0, count => 5 );
print “\n”;
$te->parse($capture);
my $table = $te->first_table_found;
foreach my $ts ($te->tables)
{
#print “Table found at “, join(’,', $ts->coords), “:\n”;
#print @$row;
$count = 0;
foreach $row ($ts->rows) {
$a = @$row->[0];
$b = @$row->[1];
$c = @$row->[2];
$d = @$row->[3];
$e = @$row->[4];
$f = @$row->[5];
$g = @$row->[6];
$h = @$row->[7];
$i = @$row->[8];
#print “$a,$b,$c,$d,$e,$f,$g,$h,$i\n”;
if ($count > 0) { ###skip first line
printXML();
}
$count++
#print join(’,', @$row), “\n”;
}
}

print “\n”;
sub printXML {
print “\t\n”;
print “\t\t$a\n” .
“\t\t\n” .
“\t\t$c\n” .
“\t\t$d\n” .
“\t\t$e\n” .
“\t\t$f\n” .
“\t\t$g\n” .
“\t\t

$h\n” .
“\t\t$i\n”;
print “\t\n”
}sub printXMLHeader {
print $cgi->header(-type => ‘application/xml’); # make browser expect xml
#print “\<\?xml version=\”1.0\” encoding=\”UTF-8\”\?>\n”;
}

About the Author

Greg

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>


Warning: stristr() [function.stristr]: Empty delimiter in /home/thegard5/public_html/gregjessup/wp-content/plugins/wassup/wassup.php on line 2093