获取响应将返回原始页面而不是LWP

时间:2013-09-16 11:05:28

标签: perl post lwp

我正在尝试提交表单而不填写任何内容并获取响应页面。

#!/usr/bin/perl -w

use LWP::UserAgent;
use HTTP::Request::Common;

$ua= LWP::UserAgent->new;
$req= POST('http://polling.newspoll.com.au/cgi-bin/polling/display_poll_data.pl?url_caller=&mode=trend&page=show_polls&question_set_id=4');

$res= $ua->request($req);
print $res->content;

但我只是打印原始页面(带表格)而不是回复。我做错了什么?

编辑:只需要找到传递给页面的正确参数即可。为了解决这个问题,我在结果页面上使用了Chrome开发者工具,然后转到网络标签,然后是标题。向下滚动一下,您将找到提供给页面的所有表单参数。感谢amon指出我正确的方向。

1 个答案:

答案 0 :(得分:1)

POST帮助程序不会获取包含查询参数的完整URL。您应该在arrayref中提供这些内容,例如:

my $req = POST('http://www.example.com', [ foo => 42, bar => 'baz' ]);

...如docs所示。

您还应该注意,POST参数不会作为查询参数在URL内传输,而是在POST请求的主体内传输。虽然像CGI这样的软件提供了一个抽象差异的界面,但其他软件可能没有。

另请注意,所有Perl脚本都应use strict; use warnings;

您应该考虑的另一件事是通过测试is_success并使用decoded_content方法进行正确的错误处理,该方法负责传输编码。

$res->is_success
  or die $res->status_line;

print $res->decoded_content;

修改

我分析了来源,结果如下(autogenerated):

  

我查看了http://polling.newspoll.com.au/cgi-bin/polling/display_poll_data.pl页面的来源,表单中包含<input>个:

     
      
  • <input name="url_caller" type="hidden" value="" />
  •   
  • <input name="mode" type="hidden" value="file" />
  •   
  • <input name="page" type="hidden" value="Search" />
  •   
  • <input name="keywords" type="text" />
  •   
  • <input name="show" type="text" />
  •   
  • <input src="/images/search_button.jpg" type="image" />
  •   
     

以下<select>组:

     
      
  • <select name="from_date_month"></select>,其值为(1 .. 12)
  •   
  • <select name="from_date_year"></select>,其值为(1998 .. 2013)
  •   
  • <select name="to_date_month"></select>,其值为(1 .. 12)
  •   
  • <select name="to_date_year"></select>,其值为(1998 .. 2013)
  •   
  • <select name="keytype"></select>,其值为("all", "any")
  •   
  • <select name="state"></select>,其值为("Any", "FEDERAL", "ACT", "NSW", "NT", "QLD", "SA", "TAS", "VIC", "WA")
  •   
     

此外,还提供了以下查询的链接:

     
      
  • ?url_caller=latest&state=Any&mode=file&page=Search
  •   
  • ?mode=trend&page=select_category
  •   
     

表示脚本也响应GET请求

     

我查看了http://polling.newspoll.com.au/cgi-bin/polling/display_poll_data.pl?url_caller=&mode=trend&page=show_polls&question_set_id=4页面的来源,表单中包含<input>个:

     
      
  • <input name="url_caller" type="hidden" value="trend" />
  •   
  • <input name="show" size="5" type="text" />
  •   
  • <input name="mode" type="hidden" value="trend" />
  •   
  • <input name="question_id" type="hidden" value="2403" />
  •   
  • <input name="page" type="submit" value="Display Results" />
  •   
  • <input name=".cgifields" type="hidden" value="to_date_year" />
  •   
  • <input name=".cgifields" type="hidden" value="to_date_month" />
  •   
  • <input name=".cgifields" type="hidden" value="from_date_month" />
  •   
  • <input name=".cgifields" type="hidden" value="to_date_day" />
  •   
  • <input name=".cgifields" type="hidden" value="from_date_year" />
  •   
  • <input name=".cgifields" type="hidden" value="from_date_day" />
  •   
     

以下<select>组:

     
      
  • <select name="from_date_day"></select>,其值为(1 .. 31)
  •   
  • <select name="from_date_month"></select>,其值为(0 .. 12)
  •   
  • <select name="from_date_year"></select>,其值为("Any Year", 1970 .. 2013)
  •   
  • <select name="to_date_day"></select>,其值为(1 .. 31)
  •   
  • <select name="to_date_month"></select>,其值为(0 .. 12)
  •   
  • <select name="to_date_year"></select>,其值为("Any Year", 1970 .. 2013)
  •   
     

此外,还提供了以下查询的链接:

     
      
  • ?url_caller=trend
  •   
  • ?url_caller=trend&mode=trend&page=show_polls&question_set_id=4
  •   
  • ?mode=trend&page=continue_results&question_id=2403&url_caller=trend
  •   
     

表示脚本也响应GET请求

您正在使用请求中的字段[url_caller => '', mode => "trend", page => "show_polls", question_set_id => 4]。例如。字段question_set_id不存在。 show_polls没有值page

隐藏字段不应更改,但会在预先填写时重新提交。对于select离子,从提供的列表中选择有效参数。对于文本输入,根据上下文提供合理的值。