解析这类数据

时间:2015-07-18 14:58:06

标签: perl object

我已经为API编写了一个包装器。以前我使用Perl处理基于字符串的简单GET请求到PHP脚本。

作为分析响应的一部分,我必须分析以下似乎是对象的数据。不幸的是,我不确定如何从中提取可用数据。

print Dumper对数据返回:

$VAR1 = bless( {
         '_rc' => '200',
         '_request' => bless( {
                '_uri_canonical' => bless( do{\(my $o = 'http://example.com/?list=1&token=h_DQ-3lru6uy_Zy0w-KXGbPm_b9llY3LAAAAALSF1roAAAAANxAtg49JqlUAAAAA')}, 'URI::http' ),
                '_content'       => '',
                '_uri'           => $VAR1->{'_request'}{'_uri_canonical'},
                '_method'        => 'GET',
                '_headers'       => bless( {
                       'accept-charset' => 'iso-8859-1,*,utf-8',
                       'accept'         => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
                       'cookie'         => 'GUID=cHoW3DLOljP4K9LzposM',
                       'user-agent'     => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20041107 Firefox/1.0',
                       'authorization'  => 'Basic YWRtaW46bmljb2xl',
                       'cookie2'        => '$Version="1"',
                       '::std_case'     => {
                         'cookie' => 'Cookie',
                         'cookie2' => 'Cookie2'
                               },
                       'accept-language' => 'en-US'
                     }, 'HTTP::Headers' )
              }, 'HTTP::Request' ),
         '_headers' => bless( {
                'client-peer'   => 'myip:8085',
                'content-type'  => 'text/plain',
                'cache-control' => 'no-cache',
                'connection'    => 'keep-alive',
                'client-date'   => 'Sat, 18 Jul 2015 12:41:00 GMT',
                '::std_case'    => {
                  'client-response-num' => 'Client-Response-Num',
                  'set-cookie2'         => 'Set-Cookie2',
                  'client-date'         => 'Client-Date',
                  'client-peer'         => 'Client-Peer',
                  'set-cookie'          => 'Set-Cookie'
                },
                'client-response-num' => 1,
                'content-length' => '8684'
              }, 'HTTP::Headers' ),
         '_msg' => 'OK',
         '_protocol' => 'HTTP/1.1',
         '_content' => '{"build":30470,"torrents": [
            ["043CC5FA0C741CDAD9D2E5CC20DF64A4A400FA34",136,"Epi.S01E03.720p.HDTV.x264-IMMERSE[rarbg]",690765843,39,26951680,671744,24,0,0,0,"",0,1454,0,114,2436,1,663814163,"","","Stopped","512840d7",1437022635,0,"","/mydir/Epi.S01E03.720p.HDTV.x264-IMMERSE[rarbg]",0,"0368737A",false],
            ["097AA60280AE3E4BA8741192CB015EE06BD9F992",200,"Epi.S01E04.HDTV.x264-KILLERS[ettv]",221928759,1000,221928759,8890308649,40059,0,0,0,"",0,1461,0,4395,65536,-1,0,"","","Queued Seed","512840d8",1437022635,1437023190,"","/mydir/Epi.S01E04.HDTV.x264-KILLERS[ettv]",0,"8F52310A",false]],
            "label": [],"torrentc": "350372445"
            ,"rssfeeds": []
            ,"rssfilters": []
            }
         ',
         '_msg' => 'OK',
         '_protocol' => 'HTTP/1.1'
       }, 'HTTP::Response' );

我想从返回的对象中提取以下每个字符串

097AA60280AE3E4BA8741192CB015EE06BD9F992
200
Epi.S01E04.HDTV.x264-KILLERS[ettv]

不幸的是,我对Perl中对象的理解非常简单。

返回此数据的原始代码如下所示:

my $ua = LWP::UserAgent->new();
my $response = $ua->get( $url, @ns_headers );
print Dumper($response);

我如何处理感兴趣的字符串?

1 个答案:

答案 0 :(得分:4)

如果您阅读documentation for HTTP::Response,您会看到有一个content方法,它会返回您的HTTP消息的内容,以及一个decoded_content方法,它会执行相同但如果数据恰好被压缩(在你的情况下数据是未压缩的),也会解压缩数据。

在这种情况下,看起来内容被编码为JSON数据,因此您还需要加载JSON模块以将其解码为Perl数据结构

例如

use JSON 'from_json';

my $content = from_json $response->decoded_content;
my $torrents = $content->{torrents};

for my $torrent ( @$torrents ) {
  say for @$torrent[0,1,2];
  say '';
}

输出

043CC5FA0C741CDAD9D2E5CC20DF64A4A400FA34
136
Epi.S01E03.720p.HDTV.x264-IMMERSE[rarbg]

097AA60280AE3E4BA8741192CB015EE06BD9F992
200
Epi.S01E04.HDTV.x264-KILLERS[ettv]