-2

我有一个程序可以检索特定网页的内容,但有些页面会出现错误:

Can't get http://www.sitename.com
302 Moved Temporarily at geturl.pl line 30.

该网站在浏览器上显示良好。

想知道我可以做些什么来获取内容?

我的代码非常简单,是 LWP 的标准用法,并且在大多数页面上都可以正常工作。

  my $browser = LWP::UserAgent->new(
    agent=>'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
    keep_alive=>'1'
  );
  ...
  my $response = $browser->get($url);

谢谢!

=================

更新:

  1. 是的,这是我正在使用的实际代码。是否有明确的选项可以打开以下重定向?
  2. 是的 wget 有效

谢谢

4

2 回答 2

0

我刚刚阅读了一些幻灯片,讨论了一些可以在 Perl 中执行 HTTP 的各种模块;也许您可以尝试其他方法之一,例如HTTP::Tiny

perl -MHTTP::Tiny -E '$res=HTTP::Tiny->new->get("http://www.sitename.com/"); say join "\n", map { $res->{$_} } (qw(response status reason content))'
于 2013-07-29T19:56:19.443 回答
0

The LWP::UserAgent docs indicate that the request method on the user agent will follow redirects automatically. It's unclear from this documentation if get uses the same logic.

You could use the request method by creating an HTTP::Request object. This example uses the request method:

perl -MData::Dumper -MHTTP::Request -MLWP -e '
  $request=HTTP::Request->new(GET => "http://www.google.com");
  $ua=LWP::UserAgent->new;
  print Dumper $ua->request($request);'
于 2011-10-21T07:53:51.310 回答