.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Scrappy::Scraper::Parser 3pm" .TH Scrappy::Scraper::Parser 3pm "2021-01-07" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Scrappy::Scraper::Parser \- Scrappy Scraper Data Extrator .SH "VERSION" .IX Header "VERSION" version 0.94112090 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& #!/usr/bin/perl \& use Scrappy::Scraper::Parser; \& \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->html($html); \& \& # get all links in all table rows with CSS selector \& my $links = $parser\->scrape(\*(Aqtable tr a\*(Aq); \& \& # select all links in the 2nd table row of all tables with XPATH selector \& my $links = $parser\->scrape(\*(Aq//table/tr[2]/a\*(Aq); \& \& # percision scraping ! \& # select all links in the 2nd table row ONLY with CSS selectors and focus() \& my $links = \& $parser\->select(\*(Aqtable tr\*(Aq) \& \->focus(2) \& \->scrape(\*(Aqa\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Scrappy::Scraper::Parser provides various tools for scraping/extracting information from web pages using the Scrappy framework. .SS "\s-1ATTRIBUTES\s0" .IX Subsection "ATTRIBUTES" The following is a list of object attributes available with every Scrappy::Scraper::Parser instance. .PP \fIdata\fR .IX Subsection "data" .PP The data attribute gets/sets the extracted data which is returned from the scrape method. .PP .Vb 3 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select(\*(Aqtable tr\*(Aq); \& $parser\->data; .Ve .PP \fIhtml\fR .IX Subsection "html" .PP The html attribute gets/sets the \s-1HTML\s0 content to be parsed and extracted from. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->html($HTML); .Ve .PP \fIhtml_tags\fR .IX Subsection "html_tags" .PP The html_tags attribute gets a hashref of all known \s-1HTML\s0 tags and attributes to be used with Web::Scraper. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->html_tags; .Ve .PP \fIworker\fR .IX Subsection "worker" .PP The worker attribute holds the Web::Scraper object which is used to parse \s-1HTML\s0 and extract data. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->worker; .Ve .SH "METHODS" .IX Header "METHODS" .SS "filter" .IX Subsection "filter" The filter method allows you to filter the tags returned within the results by supplying the filter method with a list of tag attributes that you specifically want to return, forsaking all others, including the special text and html tags/keys. .PP .Vb 4 \& # filter results and only return meta tags with a content attribute \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select(\*(Aqmeta\*(Aq); \& print $parser\->data; \& \& ... \& \& { \& name => \*(Aq...\*(Aq, \& text => \*(Aq...\*(Aq, \& html => \*(Aq...\*(Aq, \& content => \*(Aq....\*(Aq, \& http => \*(Aq...\*(Aq, \& .... \& } \& \& print $parser\->filter(\*(Aqname\*(Aq, \*(Aqcontent\*(Aq)\->data; \& \& ... \& \& { \& name => \*(Aq...\*(Aq, \& content => \*(Aq....\*(Aq, \& } .Ve .SS "focus" .IX Subsection "focus" The focus method is used zero-in on specific blocks of \s-1HTML\s0 so the selectors only extract data from within the highlighted block. The focus method is meant to be used after the select method extracts rows of data, the focus method is passed an array index which zeros-in on that row of data. .PP .Vb 1 \& my $parser = Scrappy::Scraper::Parser\->new; \& \& # percision scraping ! \& # select all links in the 2nd table row ONLY \& my $links = \& $parser\->select(\*(Aqtable tr\*(Aq) \& \->focus(2) \& \->scrape(\*(Aqa\*(Aq); .Ve .SS "scrape" .IX Subsection "scrape" The scrape method is used to extract data from the specified \s-1HTML\s0 and return the extracted data. This method is dentical to the select method with the exception of what is returned. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& my $links = $parser\->scrape(\*(Aqa\*(Aq, $from_html); #get all links .Ve .SS "select" .IX Subsection "select" The select method is used to extract data from the specified \s-1HTML\s0 and return the parser object. The data method can be used to access the extracted information. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select(\*(Aqa\*(Aq, $from_html); #get all links \& \& my $links = $parser\->data; .Ve .SS "first" .IX Subsection "first" The first method is used to return the first element from the extracted dataset. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select(\*(Aqa\*(Aq, $from_html); #get all links \& \& my $first_link = $parser\->first; \& \& # equivalent to ... \& my $first_link = $parser\->data\->[0]; .Ve .SS "last" .IX Subsection "last" The last method is used to return the last element from the extracted dataset. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select(\*(Aqa\*(Aq, $from_html); #get all links \& \& my $last_link = $parser\->last; \& \& # equivalent to ... \& my $last_link = $parser\->data\->[(@{$parser\->data}\-1)]; .Ve .SS "select_first" .IX Subsection "select_first" The select_first method is a convenience feature combining the \fBselect()\fR and \fBfirst()\fR methods to return the first element from the extracted data. .PP .Vb 3 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select_first(\*(Aqa\*(Aq); #get link text \& $parser\->select_first(\*(Aqa\*(Aq, \*(Aqhref\*(Aq); #get link URL .Ve .SS "select_last" .IX Subsection "select_last" The select_last method is a convenience feature combining the \fBselect()\fR and \fBlast()\fR methods to return the last element from the extracted data. .PP .Vb 3 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select_last(\*(Aqa\*(Aq); #get link text \& $parser\->select_last(\*(Aqa\*(Aq, \*(Aqhref\*(Aq); #get link URL .Ve .SS "each" .IX Subsection "each" The each method is used loop through the extracted dataset. The each method takes one argument, a code reference, and is passed the each extracted item. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& $parser\->select(\*(Aqa\*(Aq, $from_html); #get all links \& \& $parser\->each(sub{ \& print shift\->{href} . "\en" \& }); .Ve .SS "has_html" .IX Subsection "has_html" The has_html method return a boolean which determine whether \s-1HTML\s0 content has been set. .PP .Vb 2 \& my $parser = Scrappy::Scraper::Parser\->new; \& print \*(Aqoh no\*(Aq unless $parser\->has_html; .Ve .SH "AUTHOR" .IX Header "AUTHOR" Al Newkirk .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2010 by awncorp. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.