Booze aside, are you using Mojo::DOM for this? Below is a very short example (it's way too early on a Sunday, and I'm very low on sleep) based upon this:

#!/usr/bin/perl use strict; use warnings; use Mojo::DOM; my $html = <<"END_HTML"; <html> <head> <title>test</title> </head> <body> <ul> <li><a href="http://perlmonks.org" class="first">perlmonks</a></li> <li><a href="http://archive.org" class="second">archnive.org</a></li> <li><a href="http://sitedoesnotexist9999.net" class="third">fakesite</ +a></li> </ul> </body> </html> END_HTML # Create DOM my $dom = Mojo::DOM->new( $html ); # Find the first 'a' tag with 'second' class, remove the parent tag $dom->find('a.second')->first->parent->remove; print $dom->content;

Output:

<html> <head> <title>test</title> </head> <body> <ul> <li><a class="first" href="http://perlmonks.org">perlmonks</a></li> <li><a class="third" href="http://sitedoesnotexist9999.net">fakesite</ +a></li> </ul> </body> </html>

So the output removes the <li> containing the URL selected. If you want to keep it remove ->parent from the code above and only the URL will be removed:

<html> <head> <title>test</title> </head> <body> <ul> <li><a class="first" href="http://perlmonks.org">perlmonks</a></li> <li></li> <li><a class="third" href="http://sitedoesnotexist9999.net">fakesite</ +a></li> </ul> </body> </html>

Replace 'first' for whatever (the Mojo::DOM docs are super helpful) if you want to parse a whole page for your selector. Either use eval or Try::Tiny to warn/log/ignore failure to match.

Personally I find Mojo::DOM easy to read and deal with all sorts of crazy HTML/XML issues.


In reply to Re: safe navigation in perl? by marto
in thread safe navigation in perl? by morgon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.