Why can't I fetch www.google.com with Perl's LWP::Simple?

a 夏天 提交于 2019-12-02 10:22:29

You should examine the response code to see what's happening (you're already checking for 404s). I get a 302 - a redirect.

For example:

die "get failed ($responseCode): " . $href if (!defined $content);

Resulting message:

get failed (302): http://www.google.com at goog.pl line 20.
Dave Cross

A couple of thoughts.

1/ You seems to be using the string comparison operators (le, ne) to compare numbers. You should use the numeric comparison operators (<=, !=) instead.

2/ The value you get back from the LWP::UserAgent::get call is an HTTP::Response object. Judicious use of that class's "is_foo" method might make your code a bit cleaner.

I don't know if either of these will solve your problem. But they'll improve the quality of your code.

Here's your problem:

my $content = LWP::Simple->get($href);

That passes the string "LWP::Simple" as the first argument to 'get'. You want:

my $content = LWP::Simple::get($href);

Check your SELinux settings.

SELINUX enabled systems will not allow an outgoing connection from a web agent (httpd).

This page can tell you more about SELinux and HTTPD settings: http://wiki.centos.org/TipsAndTricks/SelinuxBooleans

Enable outbound web connections from Apache in a Perl script:

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