GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Scrappy::Scraper::Parser(3) User Contributed Perl Documentation Scrappy::Scraper::Parser(3)

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

version 0.94112090

    #!/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');

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

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;

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 => '....',
        }

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');

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

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;

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];

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)];

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

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

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"
        });

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;

Al Newkirk <awncorp@cpan.org>

This software is copyright (c) 2010 by awncorp.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

2011-07-28 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.