0

给定股票代码列表,我正在使用 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循环会创建一个空文件。

4

1 回答 1

2

这里有两个问题:

1)chomp返回删除的字符数。但是您正在设置$symbol. chomp它应该是这样的:

chomp;
$symbol = $_;

澄清@Vin 的评论

你甚至可以这样做:

$symbol = $_;
chomp($symbol);

但是,你不应该这样做:

$symbol = chomp($_);

因为chomp($_) 删除换行符,$_但它会返回删除的字符数。

2) 如果您将符号放在输入文件中的一行上,那么 $symbol 最终可能是一个包含多个符号的字符串。因此,您可能需要将它们分开或要求每个符号都在自己的行上

于 2013-09-15T05:26:08.123 回答