| 1 | package WWW::Crawl; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use HTTP::Tiny 0.083; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | our $VERSION = '0.1'; | |||||||||||||||||||||||||||
| 12 | 3 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # TODO: | ||||||||||||||||||||||||||||
| 15 | # 1 - Use HTML Parser instead of regexps | ||||||||||||||||||||||||||||
| 16 | # - we don't do this as it doesn't parse JS scripts and files | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 0 | my %attrs = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $attrs{'agent'} //= "Perl-WWW-Crawl-$VERSION"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$attrs{"agent"} //= "Perl-WWW-Crawl-$WWW::Crawl::VERSION"
MC/DC0%0/1
$attrs{"agent"} //= "Perl-WWW-Crawl-$WWW::Crawl::VERSION"
$attrs{"agent"}
Truth table0%0/2
A
A= $attrs{"agent"}
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | $attrs{'http'} = HTTP::Tiny->new( | ||||||||||||||||||||||||||||
| 26 | 0 | 'agent' => $attrs{'agent'}, | |||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | return bless \%attrs, $class; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub crawl { | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my ($self, $url, $callback) = @_; | ||||||||||||||||||||||||||
|
Subroutine
crawl: not called CC=29 SCAR=67.7
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $url = "https://$url" if $url =~ /^www/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | my $uri = URI->new($url); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | croak "WWW::Crawl: No valid URI" unless $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my (%links, %parsed); | |||||||||||||||||||||||||||
| 40 | 0 | $links{$url} = 1; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $page; | |||||||||||||||||||||||||||
| 43 | 0 | my $flag = 1; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | while (scalar keys %links and $flag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar keys %links and $flag
MC/DC0%0/2
scalar keys %links and $flag
scalar keys %links
$flag
Truth table0%0/3
A and B
A= scalar keys %linksB= $flag
| |||||||||||||||||||||||||||||
| 45 | 0 | my $url = (keys(%links))[0]; | |||||||||||||||||||||||||||
| 46 | 0 | delete $links{$url}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | next if $parsed{$url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $parsed{$url}++; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $resp = $self->{'http'}->request('GET', $url); | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | next if $resp->{'status'} == 404; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if (!$resp->{'success'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | croak "WWW::Crawl: HTTP Response " . $resp->{'status'} . " - " . $resp->{'reason'} . "\n" . $resp->{'content'}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | $page = $resp->{'content'}; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | while ($page =~ /href *?= *?("|')(.*?)('|")/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | my $link = URI->new($2)->abs($uri)->canonical; | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/
$link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
| |||||||||||||||||||||||||||||
| 62 | 0 | my $address = $link->as_string; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | while ($address =~ s/(\/|#)$//) {} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $links{$address}++ unless $link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/
MC/DC0%0/2
$link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/
$link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/
$address =~ /#/
Truth table0%0/3
A or B
A= $link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/B= $address =~ /#/
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | # Find forms | ||||||||||||||||||||||||||||
| 68 | 0 | pos($page) = 0; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | while ($page =~ /<form .*?action *?= *?("|')(.*?) *('|")/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | my $link = URI->new($2)->abs($uri)->canonical; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/
$link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
| |||||||||||||||||||||||||||||
| 72 | 0 | my $address = $link->as_string; | |||||||||||||||||||||||||||
| 73 | 0 | $links{$address}++ ; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | # Find external JS files | ||||||||||||||||||||||||||||
| 77 | 0 | pos($page) = 0; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | while ($page =~ /<script .*?src *?= *?("|')(.*?) *('|")/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | my $link = URI->new($2)->abs($uri)->canonical; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/
$link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
| |||||||||||||||||||||||||||||
| 81 | 0 | my $address = $link->as_string; | |||||||||||||||||||||||||||
| 82 | 0 | my $timestamp = $self->{'timestamp'}; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | $timestamp = '' unless $timestamp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $address =~ s/(\?|\&)$timestamp=.*(\?|\&|$)//; | |||||||||||||||||||||||||||
| 85 | 0 | $links{$address}++; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | # Find JS window.open links | ||||||||||||||||||||||||||||
| 89 | 0 | pos($page) = 0; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | while ($page =~ /(window|document).open\( *("|')(.*?)('|")/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my $link = URI->new($3)->abs($uri)->canonical; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/
$link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
| |||||||||||||||||||||||||||||
| 93 | 0 | my $address = $link->as_string; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | while ($address =~ s/(\/|#)$//) {} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $links{$address}++ unless $link->path =~ /\.(pdf|css|png|jpg|svg)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | &$callback($url) if $callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | $flag = 0 if $self->{'nolinks'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | return keys %links if $self->{'nolinks'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | return keys %parsed; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 NAME | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | WWW::Crawl - A simple web crawler for extracting links and more from web pages | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 VERSION | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | This documentation refers to WWW::Crawl version 0.1. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | use WWW::Crawl; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | my $crawler = WWW::Crawl->new(); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | my $url = 'https://example.com'; | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | my @visited = $crawler->crawl($url, \&process_page); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub process_page { | ||||||||||||||||||||||||||||
| 129 | my $url = shift; | ||||||||||||||||||||||||||||
| 130 | print "Visited: $url\n"; | ||||||||||||||||||||||||||||
| 131 | # Your processing logic here | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | The C<WWW::Crawl> module provides a simple web crawling utility for extracting links and other resources from web pages within a single domain. It can be used to recursively explore a website and retrieve URLs, including those found in HTML href attributes, form actions, external JavaScript files, and JavaScript window.open links. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | C<WWW::Crawl> will not stray outside the supplied domain. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 new(%options) | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Creates a new C<WWW::Crawl> object. You can optionally provide the following options as key-value pairs: | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =over 4 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item * | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | C<agent>: The user agent string to use for HTTP requests. Defaults to "Perl-WWW-Crawl-VERSION" where VERSION is the module version. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | C<timestamp>: If a timestamp is added to external JavaScript files to ensure the latest version is loaded by the browser, this option prevents multiple copied of the same file being indexed by ignoring the timestamp query parameter. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | C<nolinks>: Don't follow links found in the starting page. This option is provided for testing and prevents C<WWW::Crawl> following the links it finds. It also affects the return value of the L<crawl|WWW::Crawl#crawl($url,-[$callback])> method. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 METHODS | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 crawl($url, [$callback]) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Starts crawling the web starting from the given URL. The C<$url> parameter specifies the starting URL. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | The optional C<$callback> parameter is a reference to a subroutine that will be called for each visited page. It receives the URL of the visited page as an argument. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | The C<crawl> method will explore the provided URL and its linked resources. It will also follow links found in form actions, external JavaScript files, and JavaScript window.open links. The crawling process continues until no more unvisited links are found. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | In exploring the website, C<crawl> will ignore links to the following types of file C<.pdf>, C<.css>, C<.png>, C<.jpg>, C<.svg> and C<.webmanifest> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Returns an array of URLs that were parsed during the crawl. Unless the C<nolinks> option is passed to L<new|WWW::Crawl#new(%options)>, then it returns an array of links found on the itial page. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Ian Boddison, C<< <bod at cpan.org> >> | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head1 BUGS | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Please report any bugs or feature requests to C<bug-www-crawl at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 179 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Crawl>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 180 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | perldoc WWW::Crawl | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | You can also look for information at: | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =over 4 | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =item * GitHub | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | L<https://github.com/IanBod/WWW-Crawl> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Crawl> | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | L<https://metacpan.org/release/WWW-Crawl> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | This software is Copyright (c) 2023 by Ian Boddison. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | This program is released under the following license: | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Perl | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 3 | 1; # End of WWW::Crawl | |||||||||||||||||||||||||||