i'm trying to get data out of a web page. i figured i'd use web::scraper (it looked easy enough) so, i wrote this:

my $pagedata = scraper { process '//*/table[@class="someclass"]', 'table[]' => scraper { process '//tr/td[1]', 'name' => 'TEXT'; process '//tr/td[2]', 'attr' => 'TEXT'; }; }; ....... my $res = $pagedata->scrape( $content ) or die "Can't define content to parser $!"; print Dumper( $res );

and, data dumper says i'm only getting the first line of the table. where the table would look something like this and i want all of it:

<table class="someclass" style="width:508px;" id="Any_20"> <tbody> <tr> <td>name</td> <td>attribute</td> <td>name2</td> <td>attribute2</td> <td>possible name3</td> <td>possible attribute3</td> <td> .... </tr><tr> <td>... etc

so, i'm guessing that the question is how to set the //td/tr xpath strings up so that i get multiple rows? or if not, how do i setup the scraper function so that it loops to the next row? or what other module might i try to accomplish this?

UPDATE

ok, so thanks to the anonymous poster, here's what i've come up with that works damn nice:

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use LWP::Simple; use Web::Scraper; use Data::Dumper::Simple; my( $infile ) = $ARGV[ 0 ] =~ m/^([\ A-Z0-9_.-]+)$/ig; my $pagedata = scraper { process '//*/table[@class="someclass"]//tr', 'table[]' => scraper { my $count = 1; process '//tr/td[' . $count++ . ']', 'name' => 'TEXT'; process '//tr/td[' . $count++ . ']', 'attr' => 'TEXT'; }; }; open( FILE, "< $infile" ); my $content = do { local $/; <FILE> }; my $res = $pagedata->scrape( $content ) or die "Can't define content to parser $!"; print Dumper( $res );

thanks all, and sorry about not posting full code and just the snip - tried not to make the post too long.


In reply to web::scraper using an xpath by ag4ve

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.