techtween has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I need to parse a HTML file in my disk and add content to it in between UL tags and create another file and write out the output into it. But the thing is, I need to insert content in all UL tags except the first and last UL tag. I tried html::Parser and HTML::TokeParser, but some error in reading file occurs. Here is the file and code I used. Kindly help..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="keywords" content="" /> <title>My Page</title> </head> <body> <div class="faq"> <ul id="faq"> <li> <a href="#quickglance" >At a quick glance </a></li> <li><a href="#page1">intro </a></li> </ul> <p class="border"> </p> <h2><a name="quickglance">At a glance</a><br /> <br /> </h2> <ul> <li class="orangeTxt1"><a name="a1" id="a"><strong>How doe +s it work?</strong></a></li> <li><strong>Step 1:</strong> Commitment.</li> </ul> </p> <div class="border"> </div> <ul> <li class="orangeTxt1"><a name="a2" id="a2"><strong>Who ma +nages it?</strong></a></li> <li> owner actively commits by publicizing it to his/her + friends. </li> </ul> <p><strong>If you have other questions, please email us at:</strong> +</p> </div> <div class="footer"> <div class="footerLinks"> <ul> <li><a href="aboutus.html"> About us</a></li> <li> <a href="contact.html">Contact Us</a></li> </ul> </div> </div> </body> </html>
The Code I used for this is,
use strict; use warnings; our $content=''; our $val=''; our $count = 1; open (MYFILE, 'C:\Program Files\Perl Express\Scripts\faq.html'); print "The HTML File Contents are,\n"; my $mycount = 1; while ($content = <MYFILE>) { chop $content; $val = $content; my $ins = qq{ <span class="backTotop"> <a href="#a$count">Back to top</a></span> <div class="borderGoal"> </div> }; if ($val =~ m/<\/ul>/) { $count = $count + 1; } $val =~ s{(</ul>)}{$ins$1}gsi; if (-e "../faq1.html") { unlink ("../faq1.html"); open (DA, ">>faq1.html") or die "$! error trying to overwrite"; print DA "$val\n"; close (DA); } else { open (DA, ">>faq1.html") or die "$! error trying to overwrite"; print DA "$val\n"; close (DA); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: need to parse a HTML file and add content to it
by moritz (Cardinal) on Jul 20, 2011 at 12:05 UTC | |
|
Re: need to parse a HTML file and add content to it
by metaperl (Curate) on Jul 20, 2011 at 18:41 UTC | |
by pemungkah (Priest) on Jul 21, 2011 at 01:58 UTC |