开发者

Parsing Just a specific HTML tag by name or ID in Perl?

开发者 https://www.devze.com 2023-01-06 08:01 出处:网络
This is something thats been hard for me to find info on, I was fortunate to get an answer on the other thing I was trying to do that relates to this (code below).. so say I am using the $content inpu

This is something thats been hard for me to find info on, I was fortunate to get an answer on the other thing I was trying to do that relates to this (code below).. so say I am using the $content input (in reality it would be a full HTML page, not just the snippet I gave below) and I want to just get the contents of the input tag that has the name or ID "hush_username". The way it is now it gives the content of all of the input tags.. the only thing that I could find on this mentioned something like incorporating this:

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

But I have been unable to get this to work.. I would greatly 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="0000开发者_JS百科000000000000"/>
</form>


Keep it simple. How about this?

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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消