in reply to Extracting HTML content between the h tags
Just another way to look at the problem:
#!/usr/bin/perl use strict; use warnings; my $start = 0; my $h2 = 0; my $keyword = ""; my $end = 0; while ( my $Content = <DATA> ) { chomp( $Content ); my $content = lc( $Content ); if ( $start == 0 ) { $start = index ( $content, '<div id="bodyContent">' ); } else { if ( $h2 == 0 ) { my $h = index ( $content, '<h2>' ); if ( $h >= 0 ) { $h2++; $keyword = substr( $Content, $h+4 ); my $tmp = + lc ( $keyword ); $end = index ( $tmp, '</h2>' ); if ( $end >= 0 ) { $keyword = substr( $keyword, 0, $end ); print "$keyword\n\n"; $h2 = 0; $keyword = ""; } } } else { $end = index ( $content, '</h2>' ); if ( $end >= 0 ) { $keyword .= substr( $Content, 0, $end ); print "$keyword\n\n"; $h2 = 0; $keyword = ""; } else { $keyword .= $Content; } } } } __DATA__ <head> </head> <body> <div id="bodyContent"> <!-- start content --> <p>Key words: Some words. </p><p>Date: 2012-01-16 </p><p>Actualised: 2008-01-08 </p><p>Commented: 05.06.2007 </p><p>Encoded: Some code. </p> <h2> <span class="mw-headline" id="Problem"> Problem </span></h2> <p>Problem description. </p><p>Another description. </p> <h2> <span class="mw-headline" id="Solution1"> Solution 1 </span></h2> <p>Solution description. </p> <h2> <span class="mw-headline" id="Solution2"> Solution 2 </span></h2> <p>Solution description. </p> <h2> <span class="mw-headline" id="Comment"> Comment. </span> </h2> <p>Text of the comment. </p><p><br /> </p> </div> <hr /> </body>
When working with HTML, you can't be sure that everything is lined up correctly. Notice I put some end-of lines before the last </h2> just to make sure it could handle multiple lines.
Another approach would be to process the HTML and extract the <h2>...</h2> into an array and then process the array to eliminate span, fonts, etc after you have complete information in each element of the array.
Good Luck!
"Well done is better than well said." - Benjamin Franklin
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extracting HTML content between the h tags
by vagabonding electron (Curate) on Aug 05, 2012 at 14:34 UTC | |
by flexvault (Monsignor) on Aug 05, 2012 at 15:14 UTC |