Solved

WWW::Mechanize tutorial

Posted on 2011-09-11
7
792 Views
Last Modified: 2012-05-12
I am new to pearl and I would like to learn about WWW::Mechanize,
can someone share me some good links or books with many examples?

Note: I didn't find many examples on http://search.cpan.org
0
Comment
Question by:pravink22
  • 5
  • 2
7 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 36519420
There are several examples here:

http://search.cpan.org/~jesse/WWW-Mechanize-1.70/lib/WWW/Mechanize/Examples.pod

I've written many scripts using WWW::Mechanize - I'll go through them and find a few examples that I can share and post them here.
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 125 total points
ID: 36519451
I use the Mac Dashboard applet from Junecloud called 'Delivery Status' to track shipments, but I'm often not at a Mac when I get the e-mail, so I whipped up this script using WWW::Mechanize to add a package to my tracking list.

Usage:

  addpkg.pl --from DeliveryCompany --item "Cool New Thing" --no 123456789012345

The most important thing is usually to create the HTTP::Cookies object, as many websites use cookies for tracking.
addpkg.pl.txt
0
 

Author Comment

by:pravink22
ID: 36556565
Urs doc is not accessible, can you reattach once again?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:nemws1
ID: 36556636
Sure thing. ;-)
addpkg.pl.txt
0
 

Author Comment

by:pravink22
ID: 36556652
When I tried to download, I am getting the following error msg:
_____________________________________
Forbidden

You don't have permission to access /incoming/2011/09_w39/501050-addpkg.pl.txt on this server.
______________________________________

Can you copy paste it?
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36556654
Hmmm... that still comes up as forbidden.  Let me just paste it.
#!/usr/bin/perl
#
# Track a package using Junecloud
#
# File: addpkg.pl
#
# Author: Nem W Schlecht
#   Copyright © 2009
#   License: Creative Commons Attribution-Share Alike
#
# Creation Date: Apr 06, 2009
#

use WWW::Mechanize;
use HTTP::Cookies;
use Getopt::Long;

my $from;
my $name;
my $track;

my $res = GetOptions("from=s" => \$from,
                     "item=s" => \$name,
                     "no=s" => \$track);

if (length($from) < 2 || length($name) < 2 || length($track) < 2) {
    print "Usage: $0 --from <shipco> -item <itemname> -no <tracknum>\n";
    print "\tShipping companies:\n";
    while (<DATA>) {
        chomp;
        s/.*value="//;
        s/".*//;
        next unless /\.com$/;
        print "\t$_\n";
    }
    exit;
}

my $url = "https://junecloud.com/ds/";
my $m = new WWW::Mechanize;
$m->cookie_jar(HTTP::Cookies->new());

$m->get($url);

$m->submit_form(
                form_number => '1',
                fields => {
                    email => 'myemail@mydomain.com',
                    password => 'S3cr3tPaSsw0rD',
                    remember => '30'
                }
);

my $from_host;
while (<DATA>) {
    chomp;
    s/.*value="//;
    s/".*//;
    next unless /\.com$/;
    if (/$from/) {
        $from_host = $_;
        last;
    }
}

$m->submit_form(
                form_number => '1',
                fields => {
                    cmd => 'add',
                    type => 'web',
                    from => $from_host,
                    name => $name,
                    no => $track
                }
);

__DATA__
<option value="www.adobe.com">Adobe (US and Canada)</option>
<option value="www.amazon.com">Amazon.com</option>
<option value="www.amazon.ca">Amazon.ca</option>
<option value="www.amazon.co.uk">Amazon.co.uk</option>
<option value="www.amazon.co.jp">Amazon.co.jp</option>
<option value="www.amazon.de">Amazon.de</option>
<option value="www.amazon.at">Amazon.at</option>
<option value="www.amazon.fr">Amazon.fr</option>
<option value="www.amazon.it">Amazon.it (coming soon)</option>
<option value="www.apple.com">Apple - by email address</option>
<option value="www.apple.com/zip">Apple - by zip code</option>
<option value="www.apple.com/japan">Apple - Japan, by email address</option>
<option value="www.apple.com/japanzip">Apple - Japan, by zip code</option>
<option value="www.apple.com/hk">Apple - Hong Kong</option>
<option value="www.aramex.com">Aramex</option>
<option value="em.canadapost.ca">Canada Post</option>
<option value="www.city-link.co.uk">City Link</option>
<option value="track.dhl-usa.com">DHL - Express</option>
<option value="www.dhlglobalmail.com">DHL - GlobalMail</option>
<option value="nolp.dhl.de">DHL - Germany</option>
<option value="www.dpd.com">DPD - Germany</option>
<option value="www.fedex.com">FedEx</option>
<option value="www.gls-germany.com">GLS</option>
<option value="checkout.google.com">Google Checkout</option>
<option value="www.hlg.de">Hermes Logistik Gruppe - Germany</option>
<option value="www.hongkongpost.com">Hongkong Post</option>
<option value="www.japanpost.jp/ems">Japan Post - EMS</option>
<option value="www.japanpost.jp/m10">Japan Post - Morning 10 Overnight Mail</option>
<option value="www.japanpost.jp/reg">Japan Post - Registered Mail</option>
<option value="www.japanpost.jp/yupack">Japan Post - Yu-Pack</option>
<option value="www.laposte.fr">La Poste (coming soon)</option>
<option value="www.ontrac.com">OnTrac</option>
<option value="www.parcelforce.com">Parcelforce</option>
<option value="www.postdanmark.dk">Post Danmark</option>
<option value="www.poste.it/posta1">Poste Italiane - Postacelere1 Plus</option>
<option value="www.poste.it/pacco1">Poste Italiane - Paccocelere1 Plus</option>
<option value="www.poste.it/maxi">Poste Italiane - Paccocelere Maxi</option>
<option value="www.poste.it">Poste Italiane - Paccocelere3</option>
<option value="www.poste.it/pacco">Poste Italiane - Paccocelere Internationale</option>
<option value="www.poste.it/ems">Poste Italiane - EMS</option>
<option value="www.posten.no">Posten - Norway</option>
<option value="www.posten.se">Posten - Sweden</option>
<option value="www.purolator.com">Purolator</option>
<option value="www.royalmail.com">Royal Mail</option>
<option value="www.sagawa-exp.co.jp">Sagawa Express</option>
<option value="www.post.ch">Swiss Post</option>
<option value="www.tnt.com">TNT - by consignment number</option>
<option value="www.tnt.com/ref">TNT - by reference number</option>
<option value="www.tntpost.nl">TNT Post - Netherlands</option>
<option value="www.ups.com">UPS</option>
<option value="www.ups-mi.net">UPS Mail Innovations</option>
<option value="www.usps.com">US Postal Service</option>
<option value="www.kuronekoyamato.co.jp">Yamato Transport</option>
<option value="other">Other</option>

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 36556661
To use WWW::Mechanize there are 2 things you should know/do.  First, if you have Firefox with the Web Developer plugin, you can easily trace out FORM names and elements, which you'll need to submit specific forms.  The other thing is to just use trial and error.  I'll often call "print $m->content();" at various stages to make sure I'm getting the desired results.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now