From: pastrufazio on
Hi all,

I'm pretty new to the perl language, so please excuse me in advance for
my lacking skill!

SCENARIO: I want to download some pdf from a website using my account
using login/password. I found WWW::Mechanize that apparently is exactly
what I need.

The usage example is clear:

use WWW::Mechanize;
use CGI;

my $cgi = CGI->new();
my $form = $cgi->Vars;

my $agent = WWW::Mechanize->new();

$agent->get('http://www.livejournal.com/login.bml');
$agent->form_number('3');
$agent->field('user',$form->{user});
$agent->field('password',$form->{password});
$agent->submit();



How have I to set username and password in the code above if the source
of my web page like the following?

Thank you very much in advance.



<form name="form1" method="post" action="login.aspx"
onsubmit="javascript:return WebForm_OnSubmit();" id="form1">

[cut]

<tr>
<td align="right"><label for="Login1_UserName">Email:</label></
td><td><input name="Login1$UserName" type="text" id="Login1_UserName" /
><span id="Login1_UserNameRequired" title="รจ richiesto uno username"
style="color:Red;visibility:hidden;">*</span>
</td>
</tr>

<tr>
<td align="right"><label for="Login1_Password">Password:</label></
td><td><input name="Login1$Password" type="password"
id="Login1_Password" /><span id="Login1_PasswordRequired" title="A
password is needed" style="color:Red;visibility:hidden;">*</span>
</td>
</tr>

<tr>
<td align="right" colspan="2"><input type="submit" name="Login1
$LoginButton" value="Enter"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(&quot;Login1$LoginButton&quot;, &quot;&quot;,
true, &quot;Login1&quot;, &quot;&quot;, false, false))"
id="Login1_LoginButton" />
</td>
</tr>
From: Tad McClellan on
pastrufazio(a)gmail.com <pastrufazio(a)gmail.com> wrote:


> SCENARIO: I want to download some pdf from a website using my account
> using login/password. I found WWW::Mechanize that apparently is exactly
> what I need.
>
> The usage example is clear:
>
> use WWW::Mechanize;
> use CGI;
>
> my $cgi = CGI->new();
> my $form = $cgi->Vars;
>
> my $agent = WWW::Mechanize->new();
>
> $agent->get('http://www.livejournal.com/login.bml');
> $agent->form_number('3');


The 3rd form on that page is NOT a login form. The 1st form on
that page is a login form.

The 3rd form on the http://www.livejournal.com/ page is a login form
though.

You should probably use:

$agent->get('http://www.livejournal.com/');
$agent->form_id('login');

to find the correct form rather than rely on a particular position on
the page (which is sure to change at some point).


> $agent->field('user',$form->{user});
> $agent->field('password',$form->{password});
> $agent->submit();


Does it work when you replace the $form values with your
hardcoded username and password?

(of course not, you are not submitting the right form)

If so, then you do not have a problem related to Mechanize.


><form name="form1" method="post" action="login.aspx"
^^^^^^^^^^

That is a strange name for a Perl program...


> <input name="Login1$UserName"

$agent->field('user',$form->{'Login1$UserName'});

> <input name="Login1$Password"

$agent->field('password',$form->{'Login1$Password'});

Your form does not use the same form field names that LiveJournal's
form uses, you need to keep straight which field name is the one
you want in each context.


Here is the most helpful bit of advise in this whole post:

You should get the login/page fetch working *from the command line*
first, and then integrate it into your web application.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
From: pastrufazio on
Il Sat, 13 Feb 2010 14:40:44 -0600, Tad McClellan ha scritto:

> You should probably use:
>
> $agent->get('http://www.livejournal.com/');
> $agent->form_id('login');
>
> to find the correct form rather than rely on a particular position on
> the page (which is sure to change at some point).
>
>
>> $agent->field('user',$form->{user});
>> $agent->field('password',$form->{password}); $agent->submit();
>
>
> Does it work when you replace the $form values with your hardcoded
> username and password?
>
> (of course not, you are not submitting the right form)
>
> If so, then you do not have a problem related to Mechanize.
>
>
You are right, your version is much stronger that the one in the example,
I'll treasure your advise!

>><form name="form1" method="post" action="login.aspx"
> ^^^^^^^^^^
>
> That is a strange name for a Perl program...
>
No sorry, I did't make myself clear! The web page is not mine: it is a
commercial site where I have an account and I want to automate my access
on it =) sorry for my imprecision.



>> <input name="Login1$UserName"
>
> $agent->field('user',$form->{'Login1$UserName'});
>
>> <input name="Login1$Password"
>
> $agent->field('password',$form->{'Login1$Password'});
>
> Your form does not use the same form field names that LiveJournal's form
> uses, you need to keep straight which field name is the one you want in
> each context.
>
Clear!

>
> Here is the most helpful bit of advise in this whole post:
>
> You should get the login/page fetch working *from the command line*
> first, and then integrate it into your web application.

Thank you very much!!

From: Tad McClellan on
pastrufazio(a)gmail.com <pastrufazio(a)gmail.com> wrote:
> Il Sat, 13 Feb 2010 14:40:44 -0600, Tad McClellan ha scritto:

>>><form name="form1" method="post" action="login.aspx"
>> ^^^^^^^^^^
>>
>> That is a strange name for a Perl program...
>>
> No sorry, I did't make myself clear! The web page is not mine:


Your program included use of the CGI module, sure you DO own
the page that submits a form to the program you posted?


> commercial site where I have an account and I want to automate my access
> on it


It would be very easy to violate their Terms of Service by doing that.

Have you looked to see if what you want to do is allowed?

http://www.livejournal.com/legal/tos.bml

http://www.livejournal.com/bots/


>> Here is the most helpful bit of advise in this whole post:
>>
>> You should get the login/page fetch working *from the command line*
>> first, and then integrate it into your web application.
>
> Thank you very much!!


Does that mean that you _will_ get it working from the command line first?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
From: pastrufazio on
Il Sat, 13 Feb 2010 15:51:49 -0600, Tad McClellan ha scritto:

> Your program included use of the CGI module, sure you DO own the page
> that submits a form to the program you posted?
>
I will write a perl script doing the work. So: the script is on my pc,
the website involved in the login is on Internet.

Sorry... unfortunately my english is crapy.

> It would be very easy to violate their Terms of Service by doing that.
>
> Have you looked to see if what you want to do is allowed?
>
>

livejournal was only an example found here

http://search.cpan.org/~PETDANCE/WWW-Mechanize-1.60/lib/WWW/Mechanize/
Examples.pod#lj_friends.cgi,_by_Matt_Cashner

now I have to check if the terms of use of the other website allow me to
do what i want to do!

> Does that mean that you _will_ get it working from the command line
> first?

Yes, and precisely _only_ from the command line! (I will insert an enty
in my crontab scheduling the script)