perl retrieving page details after mechanize::POST

此生再无相见时 提交于 2019-12-12 18:41:19

问题


I am trying to gather data from a website. Some anti-patterns make looking finding the right form objects difficult but I have this solved. I am using a post method to get around some javascript acting as a wrapper to submit the form. My problem seems to be in getting the results from the mechanize->post method.

Here's a shortened version of my code.

use strict;
use warnings;
use HTML::Tree;
use LWP::Simple;
use WWW::Mechanize;
use HTTP::Request::Common;
use Data::Dumper;
$| = 1;

my $site_url = "http://someURL"; 
my $mech = WWW::Mechanize->new( autocheck => 1 );
foreach my $number (@numbers) 
{
    my $content = get($site_url);
       $mech->get ($site_url);

    my $tree = HTML::Tree->new();

    $tree->parse($content);

    my ($title) = $tree->look_down( '_tag' , 'a' );
    my $atag = "";
    my $atag1 = "";
    foreach $atag ( $tree->look_down( _tag => q{a}, 'class' => 'button', 'title' => 'SEARCH'     )  ) 
    {
        print "Tag is ", $atag->attr('id'), "\n";
        $atag1 = Dumper $atag->attr('id');
    }

# Enter permit number in "Number" search field
    my @forms = $mech->forms;
    my @fields = ();
    foreach my $form (@forms)
    {
        @fields = $form->param;
    }
    my ($name, $fnumber) = $fields[2];
    print "field name and number is $name\n";
    $mech->field( $name, $number, $fnumber );
    print "field $name populated with search data $number\n" if $mech->success();

    $mech->post($site_url , 
    [
       '$atag1' => $number,
       'internal.wdk.wdkCommand' => $atag1,
    ]) ;

print $mech->content; # I think this is where the problem is.

}

The data I get from my final print statement is the data from teh original URL not the page the POST command should take me to. What have I done wrong?

Many Thanks

Update

I don't have Firefox installed so I'm avoiding WWW::Mechanize::Firefox intentionally.


回答1:


Turns out I was excluding some required hidden fields from my POST command.



来源:https://stackoverflow.com/questions/22650201/perl-retrieving-page-details-after-mechanizepost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!