in reply to safe navigation in perl?
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.
|
|---|