Can I do [such-and-such] with WWW::Mechanize?

If it's possible with LWP::UserAgent,then yes. WWW::Mechanize is a subclass of LWP::UserAgent, so all the wondrous magic of that class is inherited.


How do I use WWW::Mechanize through a proxy server?

See the docs in LWP::UserAgent on how to use the proxy. Short version:

    $mech->proxy(
           ['http','ftp'],'http://proxy.example.com:8000/');

or get the specs from the environment:

    $mech->env_proxy();

    # Environment set like so:
    gopher_proxy=http://proxy.my.place/
    wais_proxy=http://proxy.my.place/
    no_proxy="localhost,my.domain"
    export gopher_proxy wais_proxy no_proxy

How can I see what fields are on the forms?

Use the mech-dump utility, optionaly installed with Mechanize.

    $ mech-dump --forms http://search.cpan.org
    Dumping forms
    GET http://search.cpan.org/search
      query=
      mode=all              (option)  [*all|module|dist|author]
      <NONAME>=CPAN Search  (submit) 

How do I get Mech to handle authentication?

    use MIME::Base64;

    my $agent = WWW::Mechanize->new();
    my @args = (
        Authorization => "Basic " .
            MIME::Base64::encode( USER . ':' . PASS )
    );

    $agent->credentials( ADDRESS, REALM, USER, PASS );
    $agent->get( URL, @args );

How can I get WWW::Mechanize to execute this JavaScript?

You can't. JavaScript is entirely client-based, and WWW::Mechanize is a client that doesn't understand JavaScript. See the top part of this FAQ.


How do I check a checkbox that doesn't have a value defined?

Set it to to the value of "on".

    $mech->field( my_checkbox => 'on' );

How do I handle frames?

You don't deal with them as frames, per se, but as links. Extract them with

    my @frame_links = $mech->find_link( tag => "frame" );

How do I get a list of HTTP headers and their values?

All HTTP::Headers methods work on a HTTP::Response object which is returned by the get(), reload(), response()/res(), click(), submit_form(), and request() methods.

    my $mech = WWW::Mechanize->new( autocheck => 1 );
    $mech->get( 'http://my.site.com' );
    my $res = $mech->response();
    for my $key ( $response->header_field_names() ) {
        print $key, " : ", $response->header( $key ), "\n";
    }
arrow
arrow
    全站熱搜

    jck11 發表在 痞客邦 留言(0) 人氣()