问题描述
我正在尝试从网站收集数据.一些反模式使寻找合适的形状对象变得很困难,但是我已经解决了.我正在使用一种post方法来避开一些充当包装器来提交表单的javascript.我的问题似乎在于从mechanize-> post方法获取结果.
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.
}
我从最终打印语句中获得的数据是来自原始URL的数据,而不是POST命令应该带我到的页面.我做错了什么?
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?
非常感谢
更新
我没有安装Firefox,所以避免使用 WWW::Mechanize::Firefox
故意.
I don't have Firefox installed so I'm avoiding WWW::Mechanize::Firefox
intentionally.
推荐答案
结果是我从POST命令中排除了一些必需的隐藏字段.
Turns out I was excluding some required hidden fields from my POST command.
这篇关于机械化后:perl检索页面详细信息:: POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!