If the page is simple enough, an approach that makes the HTML parser a bit less daunting would be to use an approach based on CSS selectors. There are several modules that implement this approach, like Mojo::DOM, Web::Magic, Web::Query. Some others like Web::Scraper provide a bit more scaffolding around running data extraction.

App::scrape is a minimalistic scraper that implements the two steps of 1) fetching an HTML page and 2) extracting data according to CSS selectors. The basic invocation would be like the following (assuming your data lives in a file 1054800.html:

C:\>scrape file:///1054800.html .product-name .price Please help $20.00

So, armed with the two selectors, you can then turn from the command line tool to using the selectors with (for example) App::scrape:

#!perl -w use strict; use App::scrape 'scrape'; use LWP::Simple 'get'; use Data::Dumper; my $html= get 'file:///1054800.html'; my @info = scrape( $html, { product => '.product-name', price => '.price', }, ); print Dumper \@info; __END__ C:\>perl -w tmp.pl $VAR1 = [ { 'product' => 'Please help', 'price' => '$20.00' } ];

Note that App::scrape assumes that your data is basically tabular. It does not cope well with data with a more complex structure, and especially not well with the situation that one product maybe has no price tag.


In reply to Re: How can i have the titles and the prices? by Corion
in thread How can i have the titles and the prices? by faozhi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.