如何正确使用哈希?
How to properly use hashes?
我正在尝试使用 perl Web::Scraper
模块来抓取页面并处理各种元素。
我写了以下脚本:
use strict;
use warnings;
use Web::Scraper;
use Data::Dumper;
use URI;
my $purlToScrape='https://isohunt.to/torrents/?ihq=back+to+the+future&Torrent_sort=seeders.desc';
my $movcol = scraper {
process "td.title-row", "movcol[]" => scraper {
process "span", "title[]" => 'TEXT';
process "a", "url[]" => '@href';
};
};
my $details = $movcol->scrape(URI->new($purlToScrape));
print Dumper($details->{movcol});
输出:
$VAR1 = [
{
'url' => [
bless( do{\(my $o = 'https://isohunt.to/torrent_details/5538709/Back-to-the-Future-III-1990-720p-BrRip-x264-700MB-YIFY')}, 'URI::https' ),
bless( do{\(my $o = 'https://isohunt.to/torrents/?iht=5&age=0')}, 'URI::https' )
],
'title' => [
'Back to the Future III (1990) 720p BrRip x264 - 700MB - YIFY'
]
},
{
'url' => [
bless( do{\(my $o = 'https://isohunt.to/torrent_details/6395538/Back-to-the-Future-1985-1080p')}, 'URI::https' ),
bless( do{\(my $o = 'https://isohunt.to/torrents/?iht=5&age=0')}, 'URI::https' )
],
'title' => [
'Back to the Future (1985) [1080p]'
]
}
];
我要做的是处理每个标题元素。如何在代码中使用这些元素?
我尝试使用 print Dumper($details->{movcol}->{title});
,但这给了我错误 Not a HASH reference
$details->{movcol}
是数组引用。取消引用数组以获取标题:
for (@{$details->{movcol}}) {
print "$_->{title}[0]\n";
}
或者,只打印第一个标题:
print "$details->{movcol}[0]{title}[0]\n";
转储中的方括号表示数组,而大括号表示散列。所以你可以看到 $details->{movcol}
是一个散列数组,每个散列都有一个带有键 title
和另一个数组的值的元素。
你可以这样打印标题
my $movcol = $details->{movcol};
for my $item ( @$movcol ) {
print $item->{title}[0], "\n";
}
或者您可以使用
创建标题字符串数组
my @titles = map $_->{title}[0], @{ $details->{movcol} };
我正在尝试使用 perl Web::Scraper
模块来抓取页面并处理各种元素。
我写了以下脚本:
use strict;
use warnings;
use Web::Scraper;
use Data::Dumper;
use URI;
my $purlToScrape='https://isohunt.to/torrents/?ihq=back+to+the+future&Torrent_sort=seeders.desc';
my $movcol = scraper {
process "td.title-row", "movcol[]" => scraper {
process "span", "title[]" => 'TEXT';
process "a", "url[]" => '@href';
};
};
my $details = $movcol->scrape(URI->new($purlToScrape));
print Dumper($details->{movcol});
输出:
$VAR1 = [
{
'url' => [
bless( do{\(my $o = 'https://isohunt.to/torrent_details/5538709/Back-to-the-Future-III-1990-720p-BrRip-x264-700MB-YIFY')}, 'URI::https' ),
bless( do{\(my $o = 'https://isohunt.to/torrents/?iht=5&age=0')}, 'URI::https' )
],
'title' => [
'Back to the Future III (1990) 720p BrRip x264 - 700MB - YIFY'
]
},
{
'url' => [
bless( do{\(my $o = 'https://isohunt.to/torrent_details/6395538/Back-to-the-Future-1985-1080p')}, 'URI::https' ),
bless( do{\(my $o = 'https://isohunt.to/torrents/?iht=5&age=0')}, 'URI::https' )
],
'title' => [
'Back to the Future (1985) [1080p]'
]
}
];
我要做的是处理每个标题元素。如何在代码中使用这些元素?
我尝试使用 print Dumper($details->{movcol}->{title});
,但这给了我错误 Not a HASH reference
$details->{movcol}
是数组引用。取消引用数组以获取标题:
for (@{$details->{movcol}}) {
print "$_->{title}[0]\n";
}
或者,只打印第一个标题:
print "$details->{movcol}[0]{title}[0]\n";
转储中的方括号表示数组,而大括号表示散列。所以你可以看到 $details->{movcol}
是一个散列数组,每个散列都有一个带有键 title
和另一个数组的值的元素。
你可以这样打印标题
my $movcol = $details->{movcol};
for my $item ( @$movcol ) {
print $item->{title}[0], "\n";
}
或者您可以使用
创建标题字符串数组my @titles = map $_->{title}[0], @{ $details->{movcol} };