tt.traduto...@gmail.com wrote:
Dear all,
I can not make a search using perl.
I get the error:

Couldn't get http://www.google.com/search?q=traducao at browser.pl
line 13.

By default, the LWP family of modules sends a client identifier that begins with 'libwww-perl/'. Unfortunately Perl is also used by many 'bad guys' out there, why some server administrators - apparently Google's administrator is one of them - are banning requests with that User-Agent.

One solution is to use LWP::UserAgent directly, and set a different User-Agent.

use CGI qw(:standard);
print header;

my $url = 'http://www.google.com/search?q=traducao';
 # Just an example: the URL for the most recent /Fresh Air/ show

  use LWP::Simple;
  my $content = get $url;
  die "Couldn't get $url" unless defined $content;

  # Then go do things with $content, like this:

print $content;

You may want to replace that code with:

    use LWP::UserAgent;
    use CGI qw(:standard);

    print header(-type => 'text/html', -charset => 'UTF-8');
    #----------------------------------^^^^^^^^^^^^^^^^^^^
    # Google uses UTF-8 encoding

    my $url = 'http://www.google.com/search?q=traducao';

    my $ua = LWP::UserAgent->new;
    my $agent = $ENV{HTTP_USER_AGENT};
    ($agent) = $agent =~ /(.+)/;        # for taint mode
    $ua->agent($agent);
    my $res = $ua->get($url);
    die $res->status_line unless $res->is_success;

    print $res->content;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to