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

前端 未结 4 570
长情又很酷
长情又很酷 2021-01-26 09:00

I cant seem to get this peice of code to work:

    $self->{_current_page} = $href;
    my $response = $ua->get($href);
    my $responseCode = $response->         


        
相关标签:
4条回答
  • 2021-01-26 09:35

    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
    
    0 讨论(0)
  • 2021-01-26 09:50

    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);
    
    0 讨论(0)
  • 2021-01-26 09:52

    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.
    
    0 讨论(0)
  • 2021-01-26 09:57

    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.

    0 讨论(0)
提交回复
热议问题