Perl 的 HTML::TableExtract 看不到职业橄榄球参考页面上的所有表格

问题描述 投票:0回答:1

我正在尝试使用 HTML::TableExtract 使用 perl 从 HTML 表中提取数据。 具体来说,我正在尝试从 Pro Football Referemce 获取 2024 年巴尔的摩乌鸦队的一些冲刺统计数据。 网页在这里:

https://www.pro-football-reference.com/teams/rav/2024.htm

HTML::TableExtract 在该页面上找到四个表。 结果发现:

  • 表0,0:网页上标记为“团队统计和排名”
  • 表0,1:网页上标记为“赛程和比赛结果”
  • 表0,2:网页上标记为“团队转化”
  • 表0,3:标记为“在网页上传递

而且——就是这样! 页面上至少还有 6 或 8 个其他表格,其中包括我想要的标有“Rushing & Receiving”的表格。 我在浏览器窗口中看到这些表,并且当我查看它们时,我在页面源中看到它们。 但 HTML::TableExtract 似乎没有注意到它们。

我的代码如下:

use strict;
#use warnings;
use HTML::TableExtract;
use LWP::Simple;

my $team = 'rav';
my $html_string = 'https://www.pro-football-reference.com/teams/' . $team . '/2024.htm';
print "   Processing  $html_string\n";
print "\n";

my $download = get $html_string ;
my $rowcount = 0;

my $te = HTML::TableExtract->new();
$te->parse($download);

foreach my $ts ($te->tables) {
   print "Table ", join(',', $ts->coords), ":\n";
   $rowcount = 0;
   foreach my $row ($ts->rows) {
       if ($ts->coords > 2) {       # This part is for ouput clarity, to  
          $rowcount++;              # restrict printing to only header rows:
       }                            # 2 rows for the first 3 tables, then one
       if ($rowcount < 2){          # row for any subsequent tables
          print "   ", join(',', @$row), "\n";
       }
      $rowcount++;
   }
}
   
print "\n";

这是我得到的输出:

   Processing  https://www.pro-football-reference.com/teams/rav/2024.htm

Table 0,0:
   ,,,Tot Yds & TO,,,,,Passing,,,,,,,Rushing,,,,,Penalties,,,,,,Average Drive,,,,
   Player,PF,Yds,Ply,Y/P,TO,FL,1stD,Cmp,Att,Yds,TD,Int,NY/A,1stD,Att,Yds,TD,Y/A,1stD,Pen,Yds,1stPy,#Dr,Sc%,TO%,Start,Time,Plays,Yds,Pts
Table 0,1:
   ,,,,,,,,,,Score,,Offense,,,,,Defense,,,,,Expected Points,,
   Week,Day,Date,,,,OT,Rec,,Opp,Tm,Opp,1stD,TotYd,PassY,RushY,TO,1stD,TotYd,PassY,RushY,TO,Offense,Defense,Sp. Tms
Table 0,2:
   ,Downs,,,,,,Red Zone,,
   Player,3DAtt,3DConv,3D%,4DAtt,4DConv,4D%,RZAtt,RZTD,RZPct
Table 0,3:
   Rk,Player,Age,Pos,G,GS,QBrec,Cmp,Att,Cmp%,Yds,TD,TD%,Int,Int%,1D,Succ%,Lng,Y/A,AY/A,Y/C,Y/G,Rate,QBR,Sk,Yds,Sk%,NY/A,ANY/A,4QC,GWD,Awards

如您所见,找到了四张桌子。 最后一个是职业足球参考网站上的“传球”表。 其他桌子在哪里? 我如何在脚本中访问他们的数据?

我使用了没有指定属性的 HTML::TableExtract->new() 构造函数来获取网页上的所有表格。 脚本找到了四个;应该至少有8个左右。

perl html-table html-parsing
1个回答
1
投票

其他表似乎是由 JavaScript 创建的,它们不存在于从给定 URL 下载的 HTML 中(您可以通过在浏览器中“查看源代码”或使用以下脚本来验证它:

use XML::LibXML;

# Your script goes here

my $dom = 'XML::LibXML'->load_html(string => $download, recover => 2) or die;
my @t = $dom->findnodes('//table');
print "Table tally:", scalar @t, "\n";  # 4

如果您有 Firefox,您可以使用 Firefox::Marionette 让浏览器为您运行 JavaScript:

#!/usr/bin/perl
use warnings;
use strict;

use Firefox::Marionette;

my $team = 'rav';
my $html_string = 'https://www.pro-football-reference.com/teams/' . $team . '/2024.htm';

my $firefox = 'Firefox::Marionette'->new->go($html_string);
my $i = 0;
for my $table ($firefox->find_tag('table')) {
    ++$i;
}
print $i, "\n";

上面写着有53张桌子。现在您可以通过将

$firefox->html
提供给
$te->parse()
来开始解析它们。

WWW::Mechanize::Chrome 是另一个选择,如果您更喜欢 Chrome 而不是 Firefox。

© www.soinside.com 2019 - 2024. All rights reserved.