Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logging into a website with cookies

Status
Not open for further replies.

spyderco

Programmer
Jan 23, 2005
107
US
This question has been asked a number of times before and I've read them and actually tried doing it once a while back (different project but even then, it never did it right. It never logged in and stayed logged in).

I want to create a script that logs into Neopets.com. It has a two page login (enter your username on page 1, enter your password on the page it redirects you to). I need to STAY logged in with the cookie or session that they send out so I can run a few other page scrapes.

What module(s) would be best to login with 2 forms and stay logged in so I can navigate with my bot where it's supposed to go?

---------------

After doing a little looking around, looks like it could do it. So here's the next question..

After reading some of the docs on this module it shows how to follow_link as if you were following an HTML link.
This is a two page form, how do I then submit the information on the next page? Would I just put in the next form code (as seen below)?

Code:
#!/usr/bin/perl

use warnings;
uses strict;

use CGI qw/:standard/;

use [URL unfurl="true"]WWW::Mechanize;[/URL]
my $mech = [URL unfurl="true"]WWW::Mechanize->new();[/URL]

    $mech->submit_form(
        form_number => 1,
        fields      => {
            username    => 'user'
        }
    );

####
# the previous form redirects us to a new page/form. This
# should fill out that form the, right?
####

    $mech->submit_form(
        form_number => 1,
        fields      => {
            password    => '12345'
        }
    );


####
# at this time, cookies should be saved allowing me to use
# Mechanize to pull back ANY $url I want on Neopets.com.
####
 
I've got a script to do something similar. It needs to log into a site and then navigate a few pages, extracting data. The site requires cookies. I've posted chunks of it here so that you can see one possible approach.
Code:
...
use HTTP::Request::Common qw(POST GET);
use LWP::UserAgent;
use URI;
...
# Create a user agent object
my $browser = LWP::UserAgent->new;
$browser->agent("MyApp/0.1 ");
$browser->proxy('http', '[URL unfurl="true"]http://192.168.1.111:3128/');[/URL]
[red]$browser->cookie_jar({});[/red]
...
my $req = POST "$site/cgi-bin/knife",
   [ login=>$logname, password=>$pw ];

# Pass request to the user agent and get a response back
my $res = $browser->request($req);

# Check the outcome of the response
#if ($res->is_success) {
# print $res->content;
#} else {
# print "Bad luck this time: ", $res->message, "\n";
#}

my $url = URI->new( "$site/cgi-bin/orders" );
$url->query_form( 'action' => 'showorders' );
$res = $browser->get($url);
... [i]# and so on[/i]

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top