Source file
Symbol file
#!/usr/bin/perl -w
#
#
# yoption.pl
#
# This hack will grab option data for a list of stock symbols.
# Prints data out to the console.
#
# depends on:
# wget: utility to grab data
# symbol.file : a file with one stock symbol per line
# optionData.tmp : a temp file that is used by wget
#
#
# See:
# http://www.oreillynet.com/pub/h/1041
# http://berserk.org/hacks/1041/
#
# This code is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
@lines = `cat symbol.file`;
foreach $symbol (@lines){
chomp($symbol);
$url ="http://finance.yahoo.com/q/op?s=";
@file = `wget -q -t 2 -O - \"$url$symbol\"`;
$i=0;
$optionSymbols[0] = "";#get current options info
$done=0;
foreach $line ( @file ){
chomp($line);
#print $line;
#
# Grab all future option links
#
if ( $done == 0 && $line =~ // ){
$line =~ s/^(.*?)(.*?)<\/b>\s\|\s//g;
$line =~ s/.*?<\/a>/$1,/g;
$line =~ s/\||\s//g;
(@optList) = split(",",$line);
foreach $oi (@optList){
$optionSymbols[$i++] = $oi;
print "list: $oi\n";
}
} elsif ( $line =~ /Expire at close/ ){
$done = 1;
}
}
for($j = 0; $j <= $#optionSymbols;$j++){
sleep(2);# don't thrash the servers
print "getting $optionSymbols[$j]\n";
$u = $url.$symbol."&m=".$optionSymbols[$j];
@file = `wget -q -t 2 -O - \"$u\"`;
$i=0;
foreach $line ( @file ){
chomp($line);
if ($line =~ /^|<\/a>//g;
$line =~ s///g;
$line =~ s/<\/b>/ /g;
$line =~ s///g;
$line =~ s/\s+/ /g;
($symPrice,$sym,$last,$change,$bid,$ask,$vol,$openInterest) = split(" ",$line);
print "$sym-$symPrice -> last:$last chg:$change bid:$bid ask:$ask vol:$vol o-i:$openInterest\n";
} elsif ($line =~ /| PUT OPTIONS/){
next; # skip put options
}
}
print "\n";
}
}
exit(0);
| |