Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here's an example using Mojo::DOM, to make life easy, even with borked HTML. It replaces hrefs beginning with the selector ;widget=460, replacing them with the URL for this site.

#!/usr/bin/perl use strict; use warnings; use Mojo::DOM; use feature 'say'; # slurp from file, get from a live site via Mojolicious::UserAgent etc +... # hardcoded for example purposes my $html = '<p><a href="https://example.com">example.com</a></p><p><a +href=";widget=460" rel="noopener" target="_blank"><img alt="Advertise +r" class="banner" height="60" src="/images/articles/newsletters/Paper +-v4-468x60.jpg" style="display: block; margin-left: auto; margin-righ +t: auto;" width="468" /></a></p>'; my $dom = Mojo::DOM->new( $html ); for my $url ( $dom->find('a[href^=";widget=460"]')->each ){ $url->attr('href' => 'https://perlmonks.org'); } say $dom->content;

Output:

<p><a href="https://example/com">example.com</a></p><p><a href="https: +//perlmonks.org" rel="noopener" target="_blank"><img alt="Advertiser" + class="banner" height="60" src="/images/articles/newsletters/Paper-v +4-468x60.jpg" style="display: block; margin-left: auto; margin-right: + auto;" width="468"></a></p

Armed with this, it'd be trivial to have a list of widgets & their real urls, substitute the selector and static url value in the code above, looping through the list of widgets.

Update: See also Re: Batch remove URLs, or super search for more examples.


In reply to Re^3: Regexp for HTML by marto
in thread Regexp for HTML by gossamer

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-05-21 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found