logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Scrappy::Scraper::Parser - Scrappy Scraper Data Extrator

Author

       Al Newkirk <awncorp@cpan.org>

Description

       Scrappy::Scraper::Parser provides various tools for scraping/extracting information from web pages using
       the Scrappy framework.

   ATTRIBUTES
       The following is a list of object attributes available with every Scrappy::Scraper::Parser instance.

       data

       The data attribute gets/sets the extracted data which is returned from the scrape method.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select('table tr');
               $parser->data;

       html

       The html attribute gets/sets the HTML content to be parsed and extracted from.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->html($HTML);

       html_tags

       The html_tags attribute gets a hashref of all known HTML tags and attributes to be used with
       Web::Scraper.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->html_tags;

       worker

       The worker attribute holds the Web::Scraper object which is used to parse HTML and extract data.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->worker;

Methods

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.

           # filter results and only return meta tags with a content attribute
           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select('meta');
               print $parser->data;

               ...

               {
                   name => '...',
                   text => '...',
                   html => '...',
                   content => '....',
                   http => '...',
                   ....
               }

               print $parser->filter('name', 'content')->data;

               ...

               {
                   name => '...',
                   content => '....',
               }

   focus
       The focus method is used zero-in on specific blocks of HTML 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.

           my  $parser = Scrappy::Scraper::Parser->new;

           # percision scraping !
           # select all links in the 2nd table row ONLY
           my  $links =
               $parser->select('table tr')
                  ->focus(2)
                  ->scrape('a');

   scrape
       The scrape method is used to extract data from the specified HTML and return the extracted data. This
       method is dentical to the select method with the exception of what is returned.

           my  $parser = Scrappy::Scraper::Parser->new;
           my  $links = $parser->scrape('a', $from_html); #get all links

   select
       The select method is used to extract data from the specified HTML and return the parser object. The data
       method can be used to access the extracted information.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select('a', $from_html); #get all links

           my  $links = $parser->data;

   first
       The first method is used to return the first element from the extracted dataset.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select('a', $from_html); #get all links

           my  $first_link = $parser->first;

           # equivalent to ...
           my  $first_link = $parser->data->[0];

   last
       The last method is used to return the last element from the extracted dataset.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select('a', $from_html); #get all links

           my  $last_link = $parser->last;

           # equivalent to ...
           my  $last_link = $parser->data->[(@{$parser->data}-1)];

   select_first
       The select_first method is a convenience feature combining the select() and first() methods to return the
       first element from the extracted data.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select_first('a'); #get link text
               $parser->select_first('a', 'href'); #get link URL

   select_last
       The select_last method is a convenience feature combining the select() and last() methods to return the
       last element from the extracted data.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select_last('a'); #get link text
               $parser->select_last('a', 'href'); #get link URL

   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.

           my  $parser = Scrappy::Scraper::Parser->new;
               $parser->select('a', $from_html); #get all links

               $parser->each(sub{
                   print shift->{href} . "\n"
               });

   has_html
       The has_html method return a boolean which determine whether HTML content has been set.

           my  $parser = Scrappy::Scraper::Parser->new;
               print 'oh no' unless $parser->has_html;

Name

       Scrappy::Scraper::Parser - Scrappy Scraper Data Extrator

Synopsis

           #!/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('table tr a');

               # select all links in the 2nd table row of all tables with XPATH selector
               my  $links = $parser->scrape('//table/tr[2]/a');

               # percision scraping !
               # select all links in the 2nd table row ONLY with CSS selectors and focus()
               my  $links =
                   $parser->select('table tr')
                      ->focus(2)
                      ->scrape('a');

Version

       version 0.94112090

See Also