ag4ve has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: web::scraper using an xpath
by jethro (Monsignor) on Dec 10, 2010 at 09:54 UTC | |
by Anonymous Monk on Dec 10, 2010 at 10:17 UTC | |
by jethro (Monsignor) on Dec 10, 2010 at 10:53 UTC | |
by Anonymous Monk on Dec 10, 2010 at 11:17 UTC | |
by ag4ve (Monk) on Dec 10, 2010 at 16:09 UTC | |
by Anonymous Monk on Dec 10, 2010 at 18:52 UTC | |
Re: web::scraper using an xpath
by Corion (Patriarch) on Dec 10, 2010 at 08:46 UTC | |
Re: web::scraper using an xpath (searches)
by Anonymous Monk on Dec 10, 2010 at 15:11 UTC | |
by Anonymous Monk on Dec 10, 2010 at 15:14 UTC |