Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

substitution in textual area of HTML file

by Takamoto (Scribe)
on Mar 10, 2020 at 15:55 UTC ( #11114080=perlquestion: print w/replies, xml ) Need Help??

Takamoto has asked for the wisdom of the Perl Monks concerning the following question:

Hello, this is my first attempt at parsing an HTML file. What I am trying to do is to substitute a string in the textual part of the HTML file. I can do this quite easily with the following code. What I am missing is probably the most easiest part: I do not want to extract and substitute the textual part (like now). What I want is to substitute my string and print out the original HTML (with changed part, of course). How should I proceed? (In my real-life example, I will need to match my string over two $sentence, but I leave this level of complexity for a later stage).

use strict; use warnings; use HTML::TreeBuilder; my $str = "<ul>" . "<div class=\"txt\" style=\"position:absolute; left:84px; top:73p +x;\"><span id=\"f1\" style=\"font-size:11px;vertical-align:baseline;c +olor:rgba(0,0,0,1);\">technology of S2S translation, also known as Sp +oken Language Translation (SLT),</span></div>" . "<div class=\"txt\" style=\"position:absolute; left:44px; top:73p +x;\"><span id=\"f1\" style=\"font-size:11px;vertical-align:baseline;c +olor:rgba(0,0,0,1);\">is a new application of AI,</span></div>" . "<li>there</li>" . "<li>everyone</li>" . "</ul>" ; # Now create a new tree to parse the HTML from String $str my $tr = HTML::TreeBuilder->new_from_content($str); # And now find all <li> tags and create an array with the values. my @lists = map { $_->content_list } $tr->find_by_tag_name('span'); # And loop through the array returning our values. foreach my $sentence (@lists) { $sentence =~ s/Spoken/SUBSTITUTION/; print $sentence, "\n"; }

Replies are listed 'Best First'.
Re: substitution in textual area of HTML file
by marto (Archbishop) on Mar 10, 2020 at 16:59 UTC

    A very quick (sub optimal, I have a long commute to begin) example using Mojo::DOM:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = '<ul><div class="txt" style="position:absolute; left:84px; +top:73px;"><span id="f1" style="font-size:11px;vertical-align:baselin +e;color:rgba(0,0,0,1);">technology of S2S translation, also known as +Spoken Language Translation (SLT),</span></div><div class="txt" style +="position:absolute; left:44px; top:73px;"><span id="f1" style="font- +size:11px;vertical-align:baseline;color:rgba(0,0,0,1);">is a new appl +ication of AI,</span></div><li>there</li><li>everyone</li></ul> '; my $dom = Mojo::DOM->new( $html ); # find each span tag foreach my $id ( $dom->find('span')->each ){ # if the text contans the string Spoken if ( index($id->text, 'Spoken') > 0){ # replace the node within the DOM my $text = $id->text; $text =~ s/Spoken/Derp!/g; $id->replace( $text ); } } # print the updated DOM say $dom->content;

    Output:

    <ul><div class="txt" style="position:absolute; left:84px; top:73px;">t +echnology of S2S translation, also known as Derp! Language Translatio +n (SLT),</div><div class="txt" style="position:absolute; left:44px; t +op:73px;"><span id="f1" style="font-size:11px;vertical-align:baseline +;color:rgba(0,0,0,1);">is a new application of AI,</span></div><li>th +ere</li><li>everyone</li></ul>

      Hi Marto, nice script to start with. I just changed it a little bit to preserve the RegEx matching feature instead of the index one.

      #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = '<ul><div class="txt" style="position:absolute; left:84px; +top:73px;"><span id="f1" style="font-size:11px;vertical-align:baselin +e;color:rgba(0,0,0,1);">technology of S2S translation, also known as +Spoken Language Translation (SLT),</span></div><div class="txt" style +="position:absolute; left:44px; top:73px;"><span id="f1" style="font- +size:11px;vertical-align:baseline;color:rgba(0,0,0,1);">is a new appl +ication of AI,</span></div><li>there</li><li>everyone</li></ul> '; my $dom = Mojo::DOM->new( $html ); # find each span tag foreach my $id ( $dom->find('span')->each ){ my $text = $id->text; if ( $text =~ /Spoken/){ $text =~ s/Spoken/Derp!/g; $id->replace( $text ); } } # print the updated DOM say $dom->content;

      Now I can start working on the second phase: matching n-grams across two adjacent DOMs.

        Fewer cars on the road tonight, you could also consider:

        #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = '<ul><div class="txt" style="position:absolute; left:84px; +top:73px;"><span id="f1" style="font-size:11px;vertical-align:baselin +e;color:rgba(0,0,0,1);">technology of S2S translation, also known as +Spoken Language Translation (SLT),</span></div><div class="txt" style +="position:absolute; left:44px; top:73px;"><span id="f1" style="font- +size:11px;vertical-align:baseline;color:rgba(0,0,0,1);">is a new appl +ication of AI,</span></div><li>there</li><li>everyone</li></ul>'; my $dom = Mojo::DOM->new( $html ); $dom->find('span')->each( sub { $_->content( $_->content =~ s/Spoken/D +erp/r ) } ); say $dom->content;
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11114080]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2020-08-09 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (55 votes). Check out past polls.

    Notices?