解析此类数据 [英] Parsing this kind of data

查看:78
本文介绍了解析此类数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a wrapper for an API. Previously I'd worked on simple string-based GET requests to PHP scripts using Perl.

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

As part of analysing the response, I have to analyse the following data which appears to be an object. Unfortunately, I'm not sure how to extract usable data from this.

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' );

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

I would like to extract each of the following strings from the returned object

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

不幸的是,我对 Perl 中的对象的理解非常初级.

Unfortunately, my understanding of objects in Perl is very elementary.

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

The original code which returns this data looks like this:

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

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

How can I work on the strings that of interest?

推荐答案

如果您阅读 文档code>HTTP::Response,你会看到有一个 content 方法,它会返回你的 HTTP 消息的内容,还有一个 decoded_content 方法执行相同的操作,但如果数据恰好被压缩(在您的情况下数据未压缩)也会解压缩数据.

If you read the documentation for HTTP::Response, you will see that there is a content method, which will return the content of your HTTP message, and a decoded_content method that does the same but also decompresses the data if it happens to be compressed (in your case the data is uncompressed.)

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

In this case it looks like the content is encoded as JSON data, so you will also need to load the JSON module to decode it into a Perl data structure

例如

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]

这篇关于解析此类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆