【问题标题】:Finding a value in HTTP response content using Perl Script使用 Perl 脚本在 HTTP 响应内容中查找值
【发布时间】:2015-09-24 23:24:07
【问题描述】:

我有带有 HTTP GET 请求的 perl 脚本。 我的回复内容是这样的

$VAR1 = \'{"ResultSet": {
  "result": [
    {
      "rank": "999999",
      "term": "shampoo"
    },
    {
      "rank": "999999",
      "term": "Beauty",
      "url": "/search/results.jsp?Ntt=shampoo&N=359434"
    },
    {
      "rank": "999999",
      "term": "Baby, Kids & Toys",
      "url": "/search/results.jsp?Ntt=shampoo&N=359449"
    },

我需要来自上述响应的 url 属性,我该如何获取它。 Itried 使用像 my $content =~ m/:"url": "(...)"/; 这样的正则表达式,但我没有得到 url 值。请指导。

【问题讨论】:

  • 你用的是Dumper($scalar)还是Dumper(\$scalar)
  • 看起来在我看来是纯文本JSON,它看起来与Dumper 输出相似,但格式不完全相同。
  • @Sobrique,这正是Dumper 输出的内容。
  • 我的意思是渲染一个字符串而不是转储一个 JSON 哈希。
  • 它是对字符串的引用。我正在解决您的说法,即它看起来 类似于 与 Dumper 输出,并且它不是完全 相同的格式。

标签: regex json string perl


【解决方案1】:

那是 JSON。所以使用JSON模块来解析它:

use JSON; 
my $json = decode_json ( $response -> content ); 
foreach my $element ( @{ $json -> {ResultSet} -> {results} } ) {
    print $element -> {url},"\n"; 
}

富勒;可运行示例:

#!/usr/bin/perl

use strict;
use warnings;
use JSON;
use Data::Dumper;

my $json_str = '{
  "ResultSet": {
  "result": [
    {
      "rank": "999999",
      "term": "shampoo"
    },
    {
      "rank": "999999",
      "term": "Beauty",
      "url": "/search/results.jsp?Ntt=shampoo&N=359434"
    },
    {
      "rank": "999999",
      "term": "Baby, Kids & Toys",
      "url": "/search/results.jsp?Ntt=shampoo&N=359449"
    }
  ]
}}';

my $json = decode_json($json_str);
print Dumper $json;
foreach my $element ( @{ $json->{ResultSet}->{result} } ) {
    print $element ->{url}, "\n" if $element->{url};
}

在上面,$json_str 填补了您的内容的利基。我假设你有纯文本,上面的输出是print Dumper \$content的结果。

这样打印出来:

$VAR1 = {
          'ResultSet' => {
                           'result' => [
                                         {
                                           'rank' => '999999',
                                           'term' => 'shampoo'
                                         },
                                         {
                                           'rank' => '999999',
                                           'term' => 'Beauty',
                                           'url' => '/search/results.jsp?Ntt=shampoo&N=359434'
                                         },
                                         {
                                           'url' => '/search/results.jsp?Ntt=shampoo&N=359449',
                                           'term' => 'Baby, Kids & Toys',
                                           'rank' => '999999'
                                         }
                                       ]
                         }
        };

/search/results.jsp?Ntt=shampoo&N=359434
/search/results.jsp?Ntt=shampoo&N=359449

【讨论】:

  • 我假设 OP 从他的 HTTP 请求中返回了纯文本,因为我认为print Dumper \$content 更有可能发生。我会在答案中明确说明。
【解决方案2】:

你有一个 JSON 字符串的引用。


首先,获取 JSON。

my $json = $$content;

如果您(错误地)使用Dumper(\$content) 而不是Dumper($content),则忽略以上内容并改用以下内容:

my $json = $content;   # Or just use $content where you see $json later.

然后,使用 JSON 解析来获取数据。

use JSON::XS qw( decode_json );
my $data = decode_json($json);             # If the $json is UTF-8 (probably)
  -or-
use JSON::XS qw( );
my $data = JSON::XS->new->decode($json);   # If the $json is decoded (Unicode Code Points)

现在,获取数据很容易。

my $results = $data->{ResultSet}{result};

for my $result (@$results) {
   my $url = $result->{url}
      or next;

   print("$url\n");
}

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2011-02-13
    • 2015-05-24
    • 1970-01-01
    相关资源
    最近更新 更多