我想生成一个http请求。我相信以下代码的工作原理除了当前我的$ uri行,因为我在google网站上尝试了它(取消注释掉该行并注释掉当前的uri行)。然后我尝试了一个感兴趣的网站,它失败了。我相信它失败了,部分是因为网页上有一个javascript。我说它失败了,因为在最后一行,它不满足if语句,相反,它死了。如果它没有输入if语句,我会认为它是成功的。关于如何实现这一目标的任何建议?
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use URI::Escape;
my $uri = "https://familysearch.org/search/collection/results#count=20&query=%2Bgivenname%3ALester%20%2Bsurname%3AYoungblood%20%2Bevent_place_level_1%3AGeorgia%20%2Bevent_place_level_2%3AWhitfield%20%2Bevent_place_level_3%3ACarbondale%20%2Bgender%3AM&collection_id=2000219";
#my $uri = "https://www.google.com";
my $req = HTTP::Request->new( 'GET', $uri );
$req->header( 'Content-Type' => 'application/json' );
my $lwp = LWP::UserAgent->new;
my $res = $lwp->request($req);
if ( not $res->is_success ) { warn "Bad response :("; sleep(1); die; }
答案 0 :(得分:1)
您需要使用其他网址:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
my $json_uri =
"https://familysearch.org/search/records?count=20&query=%2Bgivenname%3ALester%20%2Bsurname%3AYoungblood%20%2Bevent_place_level_1%3AGeorgia%20%2Bevent_place_level_2%3AWhitfield%20%2Bevent_place_level_3%3ACarbondale%20%2Bgender%3AM&collection_id=2000219";
my $lwp = LWP::UserAgent->new;
my $req = HTTP::Request->new( 'GET', $json_uri );
$req->header( 'Content-Type' => 'application/json' );
my $res = $lwp->request($req);
if ( not $res->is_success ) { warn "Bad response :("; sleep(1); die; }