Forum Moderators: coopster & phranque

Message Too Old, No Replies

Script to find a hidden value and print it

what the script finds will be used by a prog im developing

         

nacodc

7:37 pm on Dec 26, 2002 (gmt 0)

10+ Year Member



Hello, i have a program developed in perl but i need a script to make the program find a hidden value in a html text document.
Ex:
<body>
.......
<input type="hidden" value='6291587' name='class_id'>
<input type="hidden" value='4' name='key5'>
<input type="hidden" value='EF3854' name='11'>
</font></form>
</body></html>
it would read the txt with the html source code and show the third line value.. for ex: print EF3854 without the ''.
I have a program that uses the post method, but it needs some keys and values...that value changes all the time so i need it to search it from an .txt(key.txt) file and them send it to the prog so it can do the Post..etc..

the part of the prog is:

var rawfile = key.txt (THIS WILL BE THE HTML SOURCE TXT FILE)
get url [berny.com...]

var 2wayproc = perl key.pl (THIS WOULD BE THE SCRIPT TO FIND THE HIDDEN VALUE)
subst KEY6 2wayread
field pass_id = 213453
field msg_id = 2666295
field key5 = 4
field 11 = $$KEY6$$ (THIS WOULD BE THE KEY I NEED)
var Referer = [berny.com...]
post url [berny.com...]
print a msg foi impressa

thanks , i really need this help...i tried everything but i could not make it work...it seems to be a simple script to get a value from a txt file...

andreasfriedrich

8:49 pm on Dec 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] nacodc.

I´m not quite sure whether you want to get the value you are after over the net or directly from a file sitting on your server.

If the former is the case then the hidden form fields will be transmitted to the server when the form is submitted by the client. You can access them just like you access any other form field.

If you want to search the text file locally then you need to open the file, search for the line you need and close the file after that. If you want to extract the value from an HTML document you might want to use HTML::Parser if your docs are rather complex since a simple RE approach may fail on certain HTML constructs.

To give you more specific advise you would need to explain what you are after a bit more clearly.

Andreas

nacodc

9:11 pm on Dec 26, 2002 (gmt 0)

10+ Year Member



You wrote"If you want to search the text file locally then you need to open the file, search for the line you need and close the file after that. If you want to extract the value from an HTML document you might want to use HTML::Parser if your docs are rather complex since a simple RE approach may fail on certain HTML constructs."

That's what i need i want a script that opens a file containing the HTML source code (it contains a lot of html codes...but what im looking for is that third line with the name '11'...The value (EF3854)for that name changes all the time so i need it to search for it...

<body>
.....
.....
<input type="hidden" value='6291587' name='class_id'>
<input type="hidden" value='4' name='key5'>
<input type="hidden" value='EF3854' name='11'>

So the script would open the file sarch print and them close......

andreasfriedrich

9:54 pm on Dec 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use LWP::UserAgent to request the document you may use HTML::Parser to parse the document as it arrives.

use HTML::Parser; 
use LWP::UserAgent;
#
my $p = HTML::Parser->new(
api_version => 3,
start_h => [sub {
return unless pop eq 'input';
my $attr = pop;
return unless $attr->{type} eq 'hidden';
my $self = shift;
$self->{__my_secret_value__} = $attr->{11};
$self->eof;
}, 'self, attr, tagname'],
);
#
my $ua = new LWP::UserAgent;
#
{my $res = $ua->request(
HTTP::Request->new(GET => $url),
sub {last unless $p->parse($_[0])});}
#
print $p->{__my_secret_value__};

If you want to use RE this should work:

$text =~ /<input type=(["'])hidden\1 value=(["'])([^\1]+)\1 name=(["'])11\4>/; 
$__my_secret_value__ = $3;

nacodc

10:22 pm on Dec 26, 2002 (gmt 0)

10+ Year Member



One of the scripts in perl that the prog uses is bellow:
_______________________________________

# Unbuffer STDOUT
$¦ = 1;

open FILE,'msgs.txt' ¦¦ die "Não achei o arquivo com as classes...\n";

@listademsgs = ();

while (<FILE>) {

if (s/(\/Scripts\/CLASSD.asp\?group=&kkey=&ID=)(\d\d\d\d\d\d\d)/$2/) {
push (@listademsgs,$2);
}

}

while (<>) {

print "$listademsgs[$_]\n";
}
close FILE;

_________________________________________________
it shows the msgs number(Id)...and prints it to the prog, so it can use it.....
the MSGS.TXT has the following in it:
part of it is

<B><pre>MSG will be run 12/29/2002</pre>
</pre>
<center><img src="/wsdocs/cyber/PR_BAR.gif" border="0" align="middle"></center>
<a href="/Scripts/CLASSD.asp?group=&kkey=&ID=6352070"> (6352070) </a><br>
<a href="/Scripts/CLASSD.asp?group=&kkey=&ID=6352075"> more classes in about 20 min. (6352075) </a><br>
<center><img src="/wsdocs/cyber/PR_BAR.gif" border="0" align="middle"></center>
<p><font size="2">Record ID=170641
</form></body></html>
</body>
</html>
So you can have an idea of what i need...
I need a script that reads the key.txt that has the
<input type="hidden" value='6291587' name='class_id'>
<input type="hidden" value='4' name='key5'>
<input type="hidden" value='EF3854' name='11'>
</font></form>
</body></html>
and prints out the EF3854 (only without '')....
OBS that value changes all the time...
Could it be possible for you to give me an ex: of the script above with what you gave me...?

andreasfriedrich

11:05 pm on Dec 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand you correctly then you need to stick the regular expression in your code like this:

while (<FILE>) {

if (s/(\/Scripts\/CLASSD.asp\?group=&kkey=&ID=)(\d\d\d\d\d\d\d)/$2/) {
push (@listademsgs,$2);
}

if (/<input type="hidden" value='([^']+)' name='11'>/) {
print "your secret value is: $1\n";
}
}

The re will look for a string that starts with <input type="hidden" value=' followed by one or more (i.e. the plus sign) characters that are not ' followed by ' name='11'>.

Hope this helps.

Andreas

nacodc

11:18 pm on Dec 26, 2002 (gmt 0)

10+ Year Member



Thanks, but do i need to put any variable in the beggining of the script..? in other words , define any variable or anything else..?
I will try...

i found the following code in the WWW...maybe it can be usefull, i tried but did not work :
..................

#!/usr/bin/perl
open(F, "<" . $ARGV[0]) or die "Could not open " . $ARGV[0] . ": " . $! . "\n";
undef $/;
my $file = <F>;
close(F);

my $last = "";
while($file =~ m/<input type="hidden" value="(.*?)" name=(.*?)>/isg) {
$last = $1;
}

print $last;

.................
Thanks again, i will try the script you gave me

nacodc

11:43 pm on Dec 26, 2002 (gmt 0)

10+ Year Member



Thanks, it worked, thanks again......
if you need anything, pls e-mail me

Thanks
again

[edited by: jatar_k at 12:03 am (utc) on Dec. 27, 2002]
[edit reason] no emails thank you [/edit]