Perl Post方法请求/表格提交
在过去的几个小时里,我一直在做这个,在浏览了大量的谷歌搜索结果后,我仍然无法让它工作 我正在编写一个perl脚本,它连接到,选择“校外/校内住宿”,单击搜索,然后打印结果 以下是我到目前为止的情况:Perl Post方法请求/表格提交,perl,form-submit,web-scraping,Perl,Form Submit,Web Scraping,在过去的几个小时里,我一直在做这个,在浏览了大量的谷歌搜索结果后,我仍然无法让它工作 我正在编写一个perl脚本,它连接到,选择“校外/校内住宿”,单击搜索,然后打印结果 以下是我到目前为止的情况: #!/user/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common qw(POST); use Crypt::SSLeay; #Since it's HTTPS my $ua = LWP:
#!/user/bin/perl
use strict; use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use Crypt::SSLeay; #Since it's HTTPS
my $ua = LWP::UserAgent->new();
my $url = 'https://web5.uottawa.ca/rezweb/search.php';
my $formdata = ['accomodationType' => '1', 'submit' => 'Search'];
my $response = $ua->post($url, $formdata);
die "Error: ", $response->status_line . "\n"
unless $response->content;
不幸的是,它仍然是表单的页面源,而不是搜索结果页面。基本上有两种路径可供选择:硬编码所需的特定表单值,并使用LWP::UserAgent将其作为post数据传递(向post()添加arrayref参数-请参阅文档),或使用其子类,它将为您提供获取空白表单、输入值字段、提交表单并获得结果的方法。 任何一种方式都可能因网站更改而中断;由您决定哪种可能性更大,哪种似乎更容易编码 您没有使用任何LWP::Simple功能;您应该删除它的
使用
更新:
当我进入页面并选择校外住宿时,它添加了更多(可选)表单字段;在该点进行过账将发送以下参数:
accomodationType 1
maximum 9999
maximumOnCampus 9999
minimum 0
minimumOnCampus 0
submit Search
zone 0
使用所有这些参数都会起作用。相关额外文档:
我通过关闭JS到达POST参数列表,他们的站点使用它进行增强并检查请求。虽然这个解决方案有效,但它是脆弱的。确保您没有违反网站的ToS或通过敲打表单来滥用表单。查看WWW::Mechanize
谢谢;我已经阅读了文档,并在网上找到了一些示例,但我仍然有一个问题(见上文,我编辑了我的原始帖子)。
use strict;
use warnings;
use LWP::UserAgent;
use Mojo::DOM;
my $ua = LWP::UserAgent->new();
my $url = 'https://web5.uottawa.ca/rezweb/search.php';
my $formdata = [
accomodationType => 1,
maximum => 9999,
maximumOnCampus => 9999,
minimum => 0,
minimumOnCampus => 0,
"search_op[]" => 5,
"search_op[]" => 6,
"search_op[]" => 7,
"search_op[]" => 8,
"search_op[]" => 9,
"search_op[]" => 10,
"search_op[]" => 11,
srch_rental_type4 => "on",
srch_rental_type5 => "on",
submit => "Search",
zone => 0,
];
my $response = $ua->post($url, $formdata);
die "Error: ", $response->status_line, "\n"
unless $response->content;
my $dom = Mojo::DOM->new( $response->decoded_content );
my @matches = map { $_->parent->parent }
grep { $_->attrs("href") =~ /search\.php\?id=\d+/ }
$dom->find("tr > td > a")->each;
for my $row ( @matches ) {
print $row->all_text, $/;
}
__END__
157 Laurier Ave E 1 Hyman Soloway Sublet 1 bedroom 0400 01/08/2011
75 Louis Pasteur Pvt 1 Brooks Sublet 1 bedroom 0650 01/08/2011
75 Louis Pasteur Pvt 1 Brooks Sublet 1 bedroom 0750 25/07/2011