script/scrape.pl untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 84 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2 use strict;
3 use App::scrape 'scrape';
4 use List::MoreUtils 'zip';
5 use LWP::Simple qw(get);
6 use Getopt::Long;
7 use Pod::Usage;
8 our $VERSION = '0.08';
9  
10 =head1 NAME
11  
12 scrape.pl - simple HTML scraping from the command line
13  
14 =head1 ABSTRACT
15  
16 This is a simple program to extract data from HTML by
17 specifying CSS3 or XPath selectors.
18  
19 =head1 SYNOPSIS
20  
21 scrape.pl URL selector selector ...
22  
23 # Print page title
24 scrape.pl http://perl.org title
25 # The Perl Programming Language - www.perl.org
26  
27 # Print links with titles, make links absolute
28 scrape.pl http://perl.org a //a/@href --uri=2
29  
30 # Print all links to JPG images, make links absolute
31 scrape.pl http://perl.org a[@href=$"jpg"]
32  
33 # print JSON about Amazon prices
34 scrape.pl https://www.amazon.de/dp/0321751043
35 --format json
36 --name "title" #productTitle
37 --name "price" #priceblock_ourprice
38 --name "deal" #priceblock_dealprice
39  
40 # print JSON about Amazon prices for multiple products
41 scrape.pl --format json
42 --url https://www.amazon.de/dp/B01J90P010
43 --url https://www.amazon.de/dp/B01M3015CT
44 --name "title" #productTitle
45 --name "price" #priceblock_ourprice
46 --name "deal" #priceblock_dealprice
47  
48 =cut
49  
50 GetOptions(
51 'help|h' => \my $help,
52 'uri:s' => \my @make_uri,
53 'no-uri' => \my $no_known_uri,
54 'sep:s' => \my $sep,
55 'format:s' => \my $format,
56 'name:s' => \my @column_names,
57 'url:s' => \my @urls,
58 'keep-url:s' => \my $url_field,
59 ) or pod2usage(2);
60 pod2usage(1) if $help;
61  
62 $format ||= 'csv';
63  
64 # make_uri can be a comma-separated list of columns to map
65 # The index starts at one
66 my %make_uri = map{ $_-1 => 1 } map{ split /,/ } @make_uri;
67 $sep ||= "\t";
68  
69 # Now determine where we get the HTML to scrape from:
70 if( ! @urls ) {
71 @urls = shift @ARGV;
72 };
73  
74 my $args;
75 if( @ARGV ) {
76 # we need columns here
77 @column_names == @ARGV
78 or die "Different number of column names and column expressions";
79  
80 $args = +{ zip @column_names, @ARGV };
81  
82 } else {
83 $args = \@ARGV;
84 }
85  
86 my @rows;
87  
88 for my $url ( @urls ) {
89 my $html;
90 if ($url eq '-') {
91 # read from STDIN
92 local $/;
93 $html = <STDIN>;
94 } else {
95 $html = get $url;
96 };
97  
98 # now fetch all "rows" from the page. We do this once to avoid
99 # fetching a page multiple times
100 push @rows, scrape($html, $args, {
101 make_uri => \%make_uri,
102 no_known_uri => $no_known_uri,
103 base => $url,
104 url_field => $url_field,
105 });
106 };
107  
108 if( 'json' eq $format ) {
109 require JSON;
110 print JSON::encode_json(\@rows);
111  
112 } else {
113 require Text::CSV_XS;
114 Text::CSV_XS->import('csv');
115 csv( in => \@rows, out => \*STDOUT, sep_char => $sep );
116 };
117  
118 =head1 DESCRIPTION
119  
120 This program fetches an HTML page and extracts nodes
121 matched by XPath or CSS selectors from it.
122  
123 If URL is C<->, input will be read from STDIN.
124  
125 =head1 OPTIONS
126  
127 =over 4
128  
129 =item B<--format>
130  
131 Output format, the default is C<csv>. Valid values are C<csv> or C<json>.
132  
133 =item B<--url>
134  
135 URL to fetch. This can be given multiple times to fetch multiple URLs in
136 one run. If this is not given, the first argument on the command line will be
137 taken as the only URL to be fetched.
138  
139 =item B<--keep-url>
140  
141 Add the fetched URL as another column with the given name in the output.
142 If you use CSV output, the URL will always be in the first column.
143  
144 =item B<--name>
145  
146 Name of the output column.
147  
148 =item B<--sep>
149  
150 Separator character to use for columns. Default is tab.
151  
152 =item B<--uri> COLUMNS
153  
154 Numbers of columns to convert into absolute URIs, if the
155 known attributes do not everything you want.
156  
157 =item B<--no-uri>
158  
159 Switches off the automatic translation to absolute
160 URIs for known attributes like C<href> and C<src>.
161  
162 =back
163  
164 =head1 REPOSITORY
165  
166 The public repository of this module is
167 L<http://github.com/Corion/App-scrape>.
168  
169 =head1 SUPPORT
170  
171 The public support forum of this program is
172 L<http://perlmonks.org/>.
173  
174 =head1 AUTHOR
175  
176 Max Maischein C<corion@cpan.org>
177  
178 =head1 COPYRIGHT (c)
179  
180 Copyright 2011-2018 by Max Maischein C<corion@cpan.org>.
181  
182 =head1 LICENSE
183  
184 This module is released under the same terms as Perl itself.
185  
186 =cut