Parsing only a specific HTML tag by name or identifier in Perl?

It was difficult for me to find the information, I was lucky to get an answer to another thing that I tried to do related to this (code below). So let's say that I use $ content (actually it would be a full HTML page, not just the snippet I gave below), and I just want to get the contents of the input tag with the name or identifier "hush_username". The way it is now gives the content of all input tags. The only thing I could find in this was mentioning something like inclusion of this:

$tag->[1]{name} and $tag->[1]{name} eq "hush_username" ;

But I could not get this to work. I would really appreciate any advice

#!/usr/bin/perl
use strict; use warnings;
use HTML::TokeParser::Simple;
$content = do { local $/; <DATA> };            
my $parser = HTML::TokeParser::Simple->new(\$content);

while ( my $tag = $parser->get_tag('input') ) {
    print $tag->as_is, "\n";
    print "####" ;
    for my $attr ( qw( type name value ) ) {
        printf qq{%s="%s"\n}, $attr, $tag->get_attr($attr);
    }
}
__DATA__
<form name="authenticationform" id="authenticationform"
    action="/authentication/login?skin=mobile&next_webapp_name=hushmail5&amp;next_webapp_url_name=m" method="post">
<input type="hidden" name="next_webapp_page" value=""/>
<p><label for="hush_username">Email address:</label><br/>
<input type="email" name="hush_username" id="hush_username" value="email@test.com"/></p>
<p><label for="hush_passphrase">Passphrase:</label><br/>
<input type="password" name="hush_passphrase" id="hush_passphrase"  maxlength="1000" value=""/></p>
<p><input type="checkbox" name="hush_remember_me" id="hush_remember_me" value="on"
/><label for="hush_remember_me">Stay signed in when I close my browser</label></p>
<p><input type="submit" value="Sign In"/></p>
<input type="hidden" name="hush_customerid" value="0000000000000000"/>
</form>
+3
source share
1

. ?

while ( my $tag = $parser->get_tag('input') ) {
  my $name = $tag->get_attr('name');
  next unless defined $name and $name eq 'hush_username';
  print "Value: ", $tag->get_attr('value'), "\n";
}
+3

Source: https://habr.com/ru/post/1753786/


All Articles