Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Parsing HTML into various files

by wfsp (Abbot)
on Aug 25, 2010 at 10:08 UTC ( [id://857136]=note: print w/replies, xml ) Need Help??


in reply to Parsing HTML into various files

For comparison, this uses HTML::TreeBuilder.

You have nine groups of seven rows so we process seven rows at a time loading the data into an AoH. I think it reads fairly well and maintaining it ought to be relatively straight forward if your HTML changes.

#! /usr/bin/perl use strict; use warnings; use Data::Dumper; use HTML::TreeBuilder; my $file_name = q{la.html}; my $t = HTML::TreeBuilder->new_from_file($file_name) or die qq{cant build tree from *$file_name*: $!}; my @trs = $t->look_down(_tag => q{tr}); my @db; while (@trs){ my @fields = splice(@trs, 0, 7); my %rec; $rec{group} = $fields[0]->as_text; $rec{type} = $fields[1]->as_text; my @tds; @tds = $fields[2]->look_down(_tag => q{td}); $rec{level} = $tds[1]->as_text; for my $field (3..5){ @tds = $fields[$field]->look_down(_tag => q{td}); $rec{$tds[0]->as_text} = $tds[1]->as_text; $rec{$tds[2]->as_text} = $tds[3]->as_text; } $rec{note} = $fields[6]->as_text; push @db, \%rec; } print Dumper \@db;
extract and the note field shortened for brevity:
$VAR1 = [ { 'Saving Throw:' => 'None', 'Casting Time:' => '1', 'Area of Effect:' => ' 10 ft.×10 ft./level path', 'Range:' => 'Touch', 'Duration:' => '3 rds. + 2 rds./level', 'note' => ' By means of this spell... or mica.', 'Components:' => 'V, S, M', 'group' => 'Detect Illusion', 'level' => '1', 'type' => '(Divination)(Mentalism)' }, { 'Saving Throw:' => 'Special', 'Casting Time:' => 'Special', 'Area of Effect:' => 'Script reader', 'Range:' => 'Touch', 'Duration:' => '1 day/level', 'note' => ' This spell enables the ....', 'Components:' => 'V, S, M', 'group' => 'Illusionary Script', 'level' => '3', 'type' => '(Illusion/Phantasm)' }, # ... <snipped> ];
Oh, and btw, 38 lines. :-)

Replies are listed 'Best First'.
Re^2: Parsing HTML into various files
by Lady_Aleena (Priest) on Aug 25, 2010 at 18:25 UTC

    For some reason, I am getting the following error. (I checked the file name this time.)

    Can't call method "as_text" on an undefined value at C:\..\perl\treebu +ilder.pl line 29.

    line 29

    $rec{$tds[2]->as_text} = $tds[3]->as_text;

    Sorry I couldn't get it to work right away.

    Update: Wait, I think I see what might be making things hinky.

    Update 2: I was working with the wrong batch of files, but even working with the right batch of files is causing the same error. I think it has something to do with the nested tables in some of the descriptions. For files without the nested tables, this works fine.

    I am thinking that the following should go first with some way of having the tables within it written into the string it creates.

    $rec{note} = $fields[6]->as_text; #put nested tables in this one. $rec{group} = $fields[0]->as_text; $rec{type} = $fields[1]->as_text;
    Have a cookie and a very nice day!
    Lady Aleena
      Ah, I only looked at the first file, I didn't realise some of the others had nested tables. It should be fairly straight forward to accomadate them. I may not have time to look at it today and I'm away for the weekend. I should be able to get back to it on Tuesday.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://857136]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found