给定股票代码列表,我正在使用 PerlWWW::Mechanize
从 Yahoo Finance 检索证券交易所。
以下代码写入文件
#!/usr/bin/perl
# program name: FindStockExchange.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange.pl A AA AA.V AAA.TO -f ~/symbol_out.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -s Symbol
# -f Output filename
# Initialize variables:
my $urlBase = 'http://finance.yahoo.com/q?s = '; # Before symbol
my $urlSuffix = '&ql = 0'; # After symbol
my $url = '';
my $oFile = '';
my $symbol = '';
my $c = '';
# Read command line options.
GetOptions(
'f=s' => \$oFile #Output filename
) or die "Incorrect usage!\n";
# Ouptput file(s)
open(OUTSYM, ">$oFile") || die "Couldn't open file $oFile, $!";
my $m = WWW::Mechanize->new(autocheck => 0);
foreach $symbol (@ARGV) {
$url = $urlBase . $symbol . $urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets </span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
}
close OUTFIL;
以下代码从输入文件中读取并创建一个空数据文件。输入文件包含以下股票代码:
A
AA
AA.V
AAA.TO
#!/usr/bin/perl
# program name: FindStockExchange2.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange2.pl -i ~/symbol_in.txt -o ~/symbol_out2.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -i Input filename
# -o Output filename
# Initialize variables:
my $urlBase = 'http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix = '&ql=0'; # After symbol
my $url = '';
my $oFile = '';
my $iFile = '';
my $symbol = '';
my $c = '';
# Read command line options.
GetOptions(
'o=s' => \$oFile, #Output filename
'i=s' => \$iFile #Input filename
) or die "Incorrect usage!\n";
# File(s)
open(OUTSYM, ">$oFile") || die "Couldn't open file $oFile, $!";
open(INSYM, "<$iFile") || die "Couldn't open file $iFile, $!";
my $m = WWW::Mechanize->new(autocheck => 0);
while (<INSYM>) {
$symbol = chomp($_);
$url = $urlBase . $symbol . $urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets </span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
}
close INSYM;
close OUTSYM;
为什么从循环更改为使用foreach
循环读取输入文件while
会产生不同的结果?
foreach
代码创建一个包含以下内容的文件:
A NYSE
AA NYSE
AA.V TSXV
AAA.TO Toronto
To-Air-Is:~ vlis
但是while
循环会创建一个空文件。