perl - 无法用perl解析html标签

标签 perl parsing www-mechanize

我正在尝试使用 perl 解析以下链接

http://www.inc.com/profile/fuhu

我正在尝试获取排名、2013 年收入和 2010 年收入等信息, 但是当使用 perl 获取数据时,我在页面源代码中得到以下和相同的显示。

 <dl class="RankTable">
<div class="dtddwrapper">
  <div class="dtdd">
    <dt>Rank</dt><dd><%=rank%></dd>
  </div>
</div>
<div class="dtddwrapper">

当我检查 Firebug 时,我得到了关注。

<dl class="RankTable">
<div class="dtddwrapper">
  <div class="dtdd">
    <dt>Rank</dt><dd>1</dd>
  </div>
</div>
<div class="dtddwrapper">

我的 Perl 代码如下。

use WWW::Mechanize;

$url  = "http://www.inc.com/profile/fuhu";
my $mech  = WWW::Mechanize->new();

$mech->get( $url );

$data = $mech->content();
print $data;

最佳答案

正如其他人所说,这不是纯 HTML,有一些 JS 魔法。数据来自动态 JSON 请求。

以下脚本打印排名并转储 $data 中可用的所有其他内容。 首先,它获取配置文件的 ID,然后发出适当的 JSON 请求,就像常规浏览器一样。

use strict;
use warnings;

use WWW::Mechanize;
use JSON qw/decode_json/;
use Data::Dumper;

my $url  = "http://www.inc.com/profile/fuhu";
my $mech  = WWW::Mechanize->new();

$mech->get( $url );

if ($mech->content() =~ /profileID = (\d+)/) {
    my $id = $1;
    $mech->get("http://www.inc.com/rest/inc5000company/$id/full_list");
    my $data = decode_json($mech->content());
    my $rank = $data->{data}{rank};

    print "rank is $rank\n";
    print "\ndata hash value \n";
    print Dumper($data);
}

输出:

rank is 1

data hash value 
$VAR1 = {
          'time' => '2014-08-22 11:40:00',
          'data' => {
                      'ifi_industry' => 'Consumer Products & Services',
                      'app_revenues_lastyear' => '195640000',
                      'industry_rank' => '1',
                      'ifc_company' => 'Fuhu',
                      'current_industry_rank' => '1',
                      'app_employ_fouryearsago' => '49',
                      'ifc_founded' => '2008-00-00',
                      'rank' => '1',
                      'city_display_name' => 'Los Angeles',
                      'metro_rank' => '1',
                      'ifc_business_model' => 'The creator of an Android tablet for kids and an Adobe Air application that allows children to access the Internet in a parent-controlled environment.',
                      'next_id' => '25747',
                      'industry_id' => '4',
                      'metro_id' => '2',
                      'app_employ_lastyear' => '227',
                      'state_rank' => '1',
                      'ifc_filelocation' => 'fuhu',
                      'ifc_url' => 'http://www.fuhu.com',
                      'years' => [
                                   {
                                     'ify_rank' => '1',
                                     'ify_metro_rank' => '1',
                                     'ify_industry_rank' => '1',
                                     'ify_year' => '2014',
                                     'ify_state_rank' => '1'
                                   },
                                   {
                                     'ify_industry_rank' => undef,
                                     'ify_year' => '2013',
                                     'ify_rank' => '1',
                                     'ify_metro_rank' => undef,
                                     'ify_state_rank' => undef
                                   }
                                 ],
                      'ifc_twitter_handle' => 'NabiTablet',
                      'id' => '22890',
                      'app_revenues_fouryearsago' => '123000',
                      'ifc_city' => 'El Segundo',
                      'ifc_state' => 'CA'
                    }
        };

关于perl - 无法用perl解析html标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25450463/

相关文章:

perl - 如何使用 Perl 的 LWP 登录 Web 应用程序?

perl - 为什么我的 Perl 诅咒窗口不起作用?

perl - Perl 使用什么散列函数/算法?

ios - 图像 URL JSON 在应用程序错误中解析为 UIImageView

java - 如何使用 Perl 识别对 Java 类的引用?

javascript - 抓取动态表单 WWW::Mechanize Perl

perl - 在哪些情况下 die() 不会退出 Perl 脚本?

php - PHP 相当于 Perl 的正则表达式替换是什么?

java - 如何在 Java 中解析字符串?有没有类似Python的re.finditer()的?

perl - WWW::Mechanize 提交上的文件名不正确