[英]Perl - Breaking a variable from the input into two for the url
我正在從.txt加載數據以進行抓取。 但是,URL要求我打破該變量並對其執行+/- 2。 例如,如果值為2342,我需要為URL的目的創建2340和2344。
我猜測如何分解它:
$args{birth_year} = ($args{birth_year} - 2) . '-' . ($args{birth_year} + 2);
我如何將其放入URL?
這是代碼的相關部分:
use strict;
use warnings;
use WWW::Mechanize::Firefox;
use Data::Dumper;
use LWP::UserAgent;
use JSON;
use CGI qw/escape/;
use HTML::DOM;
open(my $l, 'locations2.txt') or die "Can't open locations: $!";
while (my $line = <$l>) {
chomp $line;
my %args;
@args{qw/givenname surname birth_place birth_year gender race/} = split /,/, $line;
$args{birth_year} = ($args{birth_year} - 2) . '-' . ($args{birth_year} + 2);
my $mech = WWW::Mechanize::Firefox->new(create => 1, activate => 1);
$mech->get("https://familysearch.org/search/collection/index#count=20&query=%2Bgivenname%3A$args{givenname}20%2Bsurname%3A$args{surname}20%2Bbirth_place%3A$args{birth_place}%20%2Bbirth_year%3A1910-1914~%20%2Bgender%3A$args{gender}20%2Brace%3A$args{race}&collection_id=2000219");
輸入是:
Benjamin,Schuvlein,Germany,1912,M,White
所需的URL是:
為什么你不能改變這一行:
$mech->get("https://familysearch.org/search/collection/index#count=20&query=%2Bgivenname%3A$args{givenname}20%2Bsurname%3A$args{surname}20%2Bbirth_place%3A$args{birth_place}%20%2Bbirth_year%3A1910-1914~%20%2Bgender%3A$args{gender}20%2Brace%3A$args{race}&collection_id=2000219");
對此:
$mech->get("https://familysearch.org/search/collection/index#count=20&query=%2Bgivenname%3A$args{givenname}20%2Bsurname%3A$args{surname}20%2Bbirth_place%3A$args{birth_place}%20%2Bbirth_year%3A$args(birth_year)~%20%2Bgender%3A$args{gender}20%2Brace%3A$args{race}&collection_id=2000219");
注意:我改變了這一點:
%3A1910-1914~%20
對此:
%3A$arg(birth_year)~%20
一種方法:
file content:
link1
link2
...
linkn
use Data::Dumper;
use strict;
use warnings;
local $/=undef;
open(FILE,'<',$filename) or die $filename;
my $i = 1;
while (my $line = <FILE>){
chomp($line);
print "line: $line\n";
my $tempfile = './$i.html';$i++;
$mech->get( $line, ':content_file' => $tempfile, synchronize => 1 );
}
這個答案沒有考慮輸入中的數據是否需要進行URL編碼,即如果姓氏為“von Schtupp”,它需要成為“von%20Schtupp”。
我沒有對此進行測試,因此可能存在拼寫錯誤或輕微錯誤。 不過,這是我會使用的方法。 我的回答還假設您不關心搜索條件出現的順序。
my %query_params = (
givenname => $args{givenname},
surname => $args{surname},
birth_place => $args{birth_place},
birth_year => sprintf("%d-%d", $args{birth_year} - 2, $args{birth_year} + 2),
gender => $args{gender},
race => $args{race},
);
my $query_parameter = join '%20',
map { "%2B$_%3A$query_params{$_}" }
keys %query_params;
my $url = "https//familysearch.org/search/collection/index#count=20&query=" .
$query_parameter . "&collection_id=2000219";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.