【问题标题】:500 Can't connect to url with lwp in perl500 无法在 perl 中使用 lwp 连接到 url
【发布时间】:2021-03-01 02:21:51
【问题描述】:

我正在尝试使用 fandom wikia API 解析一些 json 数据。当我浏览到 my marvel.fandom.com/api request 时,我得到以下 JSON 输出:{"batchcomplete":"","query":{"pages":{"45910":{"pageid":45910,"ns":0,"title":"Uncanny X-Men Vol 1 171"}}}}

没有什么好开始的,通过在线 JSON 解析器运行它会给出以下输出:

{
   "batchcomplete":"",
   "query":{
      "pages":{
         "45910":{
            "pageid":45910,
            "ns":0,
            "title":"Uncanny X-Men Vol 1 171"
         }
      }
   }
}

据我所知,这似乎还可以

我想获得其他几个请求的 pageid,但我似乎无法通过 Perl 获得相同的输出。 脚本:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use JSON;
use Data::Dumper;

my $url = "https://marvel.fandom.com/api.php?action=query&titles=Uncanny%20X-Men%20Vol%201%20171&format=json";

my $json = getprint( $url);
die "Could not get $url!" unless defined $json;

my $decoded_json = decode_json($json);
print Dumper($decoded_json);

但这会产生以下错误:

Could not get https://marvel.fandom.com/api.php?action=query&titles=Uncanny%20X-Men%20Vol%201%20171&format=json! at ./marvelScraper.pl line 11.

当我将 get 更改为 getprint 以获取一些额外信息时,我得到了这个:

500 Can't connect to marvel.fandom.com:443 
<URL:https://marvel.fandom.com/api.php?action=query&titles=Uncanny%20X-Men%20Vol%201%20171&format=json>
malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)") at ./script.pl line 13.

我在另一台计算机上尝试过,仍然出现同样的错误。

LWP::SimpleLWP::Protocol::https 的版本

/usr/bin/perl -MLWP::Simple -E'say $LWP::Simple::VERSION'
6.15
/usr/bin/perl -MLWP::Protocol::https -E'say $LWP::Protocol::https::VERSION'
6.09

显然它与 Windows 上的 Bash Ubuntu 有关,因为在 Ubuntu 18.04 上我得到(使用相同的脚本)以下响应:

JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this) at ./test.pl line 13.
{"batchcomplete":"","query":{"pages":{"45910":{"pageid":45910,"ns":0,"title":"Uncanny X-Men Vol 1 171"}}}}

【问题讨论】:

  • 如果我将getprint 替换为get,它对我有用。 getprint 函数不返回 JSON 内容,而是返回请求的状态码。
  • 代码工作正常——获取并打印数据,但$decoded_json 将保持200 表示成功。也许您打算使用get 来将数据获取到变量中。您在测试时遇到500 Internal Server Error 的一种可能性。
  • 这很奇怪,它仍然对我不起作用。 get 函数只是给了我无法获得marvel.fandom.com/…!在 ./script.pl 第 11 行
  • @HåkonHægland 可能是 Perl 版本不同或类似的问题吗?
  • edit 提供minimal reproducible example 并回复您收到的cmets,这将有资格重新开放。

标签: json perl


【解决方案1】:

实际上,我的Bash Ubuntu on Windows 使用get() 命令而不是您在编辑问题后提供的getprint() 命令运行相同的脚本。

orabig@Windows:~/DEV$ ./so.pl
$VAR1 = {
          'query' => {
                       'pages' => {
                                    '45910' => {
                                                 'pageid' => 45910,
                                                 'ns' => 0,
                                                 'title' => 'Uncanny X-Men Vol 1 171'
                                               }
                                  }
                     },
          'batchcomplete' => ''
        };

所以也许你有另一个与 Perl 或 Ubuntu 无关的问题。

你可以试试这个吗?

curl -v 'https://marvel.fandom.com/api.php?action=query&titles=Uncanny%20X-Men%20Vol%201%20171&format=json'

也许你只是点击了网站太多,500错误只是一些防盗链保护的结果?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多