无法使用perl解析html标记

无法使用perl解析html标记,perl,parsing,www-mechanize,Perl,Parsing,Www Mechanize,我正在尝试使用perl解析以下链接 我试图获取排名、2013年收入和2010年收入等信息, 但当使用perl获取数据时,我在页面源代码中得到了如下和相同的显示 <dl class="RankTable"> <div class="dtddwrapper"> <div class="dtdd"> <dt>Rank</dt><dd><%=rank%></dd> </div>

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

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

 <dl class="RankTable">
<div class="dtddwrapper">
  <div class="dtdd">
    <dt>Rank</dt><dd><%=rank%></dd>
  </div>
</div>
<div class="dtddwrapper">
这个东西:在脚本中,它不是HTML。因此,当您在firebug中看到它时,它会在执行此部分后显示。但是当你看HTML代码时,你是这样看的。所以HTML解析在这里不起作用


通常在这种情况下,使用XHR调用从服务器传递变量(例如rank)。因此,您需要检查firebug中的XHR调用并查看响应。

正如其他人所说,这不是简单的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'
                    }
        };

签出或XML解析器该页面使用JavaScript模板,因此我认为Mechanize不适合您。您最好使用他们的JSON页面获取数据:。那么,我如何获取数据呢?我得到了5000个到Parses的链接,我可能想澄清一下
是一个(我想),以及
WWW::Mechanize
。你需要弄清楚,在哪里可以得到变量(秩)。您需要检查XHR请求(使用firebug)。无论如何,我发现::)@Himanshu是的,是一个流行的请求。谢谢,我不知道perl中的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'
                    }
        };