-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbookwalker-book-titles.pl
executable file
·46 lines (34 loc) · 1.17 KB
/
bookwalker-book-titles.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env perl
use v5.28;
use Mojo::UserAgent;
binmode STDOUT, ":utf8";
my %visited;
my @indices = ('https://www.bookwalker.com.tw/');
my $ua = Mojo::UserAgent->new;
my $res;
while(@indices) {
my $url = pop(@indices);
next if $visited{$url};
say STDERR ">>> OPEN $url";
$res = $ua->get($url)->result;
$res->dom->find("a[href*=/product/]")->each(
sub {
my $title = $_->attr("title") or return;
my $link = $_->attr("href");
return unless ($link =~ m{//www\.bookwalker\.com\.tw/product/[0-9]+$}) && (not $visited{$link});
$visited{$link} = 1;
say $link . "\t" . $title;
}
);
$res->dom->find("a[href*=/category/]")->each(
sub {
my $link = $_->attr("href");
my $link_no_params = $link =~ s/\?.+$//r;
return unless ($link_no_params =~ m{/category/[0-9]+$}) && (not $visited{$link_no_params});
$visited{$link_no_params} = 1 if $link ne $link_no_params;
# Not a mistake. Links are deduped by their param-less version but visited as-is.
push @indices, $link;
}
);
$visited{$url} = 1;
}