in reply to PERL HTML::TableExtractor
It is no secret that I really do not coding anything web related so the following is probably the wrong way to do it, but it makes your intent a whole lot more clear:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use WWW::Mechanize; use HTML::TableContentParser; use HTML::TokeParser::Simple; my $url= "http://www.servpro.com/locator/lookup.asp?stname=Alabama&sta +te=AL"; my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->get($url); my $table = HTML::TableContentParser->new()->parse($mech->content); for my $t (@$table) { next if ! want_table($t); my $cell = $t->{rows}[0]{cells}[0]{data}; my $p = HTML::TokeParser::Simple->new(\$cell); my ($name, $owner, $phone, $fax, $website); my $rec = {}; my %seq = (1 => 'name', 3 => 'name', 4 => 'owner', 5 => 'phone', 6 + => 'fax'); my $cnt; while (my $token = $p->get_token) { next if ! $token->is_text && ! $token->is_start_tag('a'); ++$cnt; my $field = $seq{$cnt}; if ($field) { $rec->{$field} .= $token->as_is if $token->is_text; next; } if ($token->is_start_tag('a')) { $rec->{website} = $token->return_attr('href'); last; } } print Dumper($rec); } sub want_table { my ($t) = @_; return if ! $t->{class}; return if $t->{class} ne 'smalltext'; return if @{$t->{rows}} > 2; return 1; }
You can replace the call to Dumper with storing a record in the database. $rec->{owner} will contain the owner information and $rec->{website} will contain the url to the website, etc, etc.
Cheers - L~R
|
|---|