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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |