gpt4 book ai didi

json - 500 无法在 perl 中使用 lwp 连接到 url

转载 作者:行者123 更新时间:2023-12-04 03:47:48 25 4
gpt4 key购买 nike

我正在尝试使用 fandom wikia API 解析一些 json 数据。当我浏览到 my marvel.fandom.com/api request我得到以下 JSON 输出:{"batchcomplete":"","query":{"pages":{"45910":{"pageid":45910,"ns":0,"title":"Uncanny X 战警第 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"}}}}

最佳答案

实际上,我的 Windows 上的 Bash Ubuntu 使用 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错误只是一些反盗链保护的结果?

关于json - 500 无法在 perl 中使用 lwp 连接到 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64882530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com