Perl WWW :: Mechanize在从数据文件中读取输入数据时不会打印结果

时间:2013-09-15 05:05:21

标签: perl printing mechanize

我使用Perl和WWW::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循环会创建一个空文件。

1 个答案:

答案 0 :(得分:2)

这里有两个问题:

1)chomp返回删除的字符数。但您将$symbol设置为chomp的结果。它应该是这样的:

chomp;
$symbol = $_;

回应@Vin

的评论

你甚至可以这样做:

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

但是,你应该这样做:

$symbol = chomp($_);

由于chomp($_) 会从$_删除换行符,但会返回已删除的字符数。

2)如果你在输入文件中的一行上放置符号,那么$符号最终可能是一个包含多个符号的字符串。所以你可能需要拆分它们或者要求每个符号都在它自己的行上