strip HTML Tags with perl

前端 未结 5 719
粉色の甜心
粉色の甜心 2020-12-17 03:02

Whats the easiest way to strip the HTML tags in perl. I am using a regular expression to parse HTML from a URL which works great but how can I strip the HTML tags off?

相关标签:
5条回答
  • 2020-12-17 03:32

    An attempt to answer your misguided question


    Problems


    It's a bad habit to get into regex'ing out HTML because there are so many rules and ways to get around them, that may eventually open your code up to hacking techniques. While you might have a legitimate need for something simple now, it is very easy to reuse code and forget why it was a bad idea to reuse it, especially when you don't add comments like # This code is NOT secure and should not be used to parse HTML anywhere else!!! or # Christina Alguilera writes songs based on this code!!!

    Example of differences in HTML that require lots of regex rules:

    <div>...</div>
    <div style="blah">
    <div style="background:url(../div)">
    <div style=".." class='noticesinglequote'>
    

    The list goes on and that's only for well-formed HTML. Some other examples of problems include:

    1. HTML elements closed improperly (eg <div><span></div></span>) or not at all
    2. Spelling errors (eg <dvi>..</div>)
    3. HTML designed with the intention to break your script
    4. Other issues: comments, whitespaces, charsets, etc

    Solution


    You may have accepted an answer, but you should look at XML::Parser and HTML::TreeBuilder.

    Rather than stripping out parts of the HTML Document, you are probably more interested in drilling down to the part of the document you want (eg everything in <body> or a certain div inside of it), which is why you most likely want something that one of the above modules provide. Not to mention, parsers can be used to do their best at removing all HTML elements and returning only text/CData.

    0 讨论(0)
  • 2020-12-17 03:35

    Have a look at the HTML::Restrict module which allows you to strip away or restrict the HTML tags allowed. A minimal example that strips away all HTML tags:

    use HTML::Restrict;
    
    my $hr = HTML::Restrict->new();
    my $processed = $hr->process('<b>i am bold</b>'); # returns 'i am bold'
    

    I would recommend to stay away from HTML::Strip because it breaks utf8 encoding.

    0 讨论(0)
  • 2020-12-17 03:40

    As mentioned, don't use regular expressions for this. There are simply too many exceptions.

    One CPAN module which can help is HTML::Strip:

    use HTML::Strip;
    
    my $hs         = HTML::Strip->new();
    my $clean_text = $hs->parse( $raw_html );
    $hs->eof;
    

    It's worth learning what's available on the CPAN and making use of it. It will save you a lot of work in the long run.

    0 讨论(0)
  • 2020-12-17 03:44

    there is also a nice Perl module HTML::Scrubber.

     #!/usr/bin/perl
     use warnings; 
     use strict;
     use HTML::Scrubber;
     my $file = shift or die "need a file $!\n";
    
     my $html;
     open (FH,"< $file");
     read( FH, $html, -s FH );
     close FH;
     #print "$html\n";
    
      my $scrubber = HTML::Scrubber->new;
      $scrubber->default(1); ## default to allow HTML
    
       #$scrubber->script(0); ## no script
       #$scrubber->style(0); ## no style
       # OR
       $scrubber->deny(qw[script style]);
    
       my $clean_html = $scrubber->scrub($html);
    
      open (OH, '>', $file.'.scrubbed.html');
      print OH $clean_html;
      close OH;
      exit;
    
    0 讨论(0)
  • 2020-12-17 03:51

    If you just want to remove HTML tags:

    s/<script.*?<\/script>//sg
    s/<.+?>//sg
    

    This will (most of the time) remove script tags and their contents, and all other HTML tags. You could also probably remove everything before the <body> tag safely with regex.

    For anything more complex than that, though, regular expressions are not a suitable tool, and you really need to parse the HTML with an actual HTML parser and then manipulate that to remove the tags.

    0 讨论(0)
提交回复
热议问题