[R] R program google search
Waverley @ Palo Alto
waverley.paloalto at gmail.com
Tue Sep 7 19:35:32 CEST 2010
Hi,
I have sent some request as how to embed the Google search API in R? I
remember on one mailing list people talked about this previously using
R.
I did some analysis on this and found that google API (SOAP based) has
retired and was replaced with the AJAX search API. I found the
following perl code which does the search using google AJAX API to
obtain the google search result? Has anyone know how to do the
equivalent in R? I am attaching the perl code as following:
Thanks much in advance.
#!/usr/bin/perl
# This example request includes an optional API key which you will need to
# remove or replace with your own key.
# Read more about why it's useful to have an API key.
# The request also includes the userip parameter which provides the end
# user's IP address. Doing so will help distinguish this legitimate
# server-side traffic from traffic which doesn't come from an end-user.
##
my $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=Paris%20Hilton&key="get your own key from
google"&userip="your machine IP"";
# Load our modules
# Please note that you MUST have LWP::UserAgent and JSON installed to use this
# You can get both from CPAN.
use LWP::UserAgent;
use JSON;
# Initialize the UserAgent object and send the request.
# Notice that referer is set manually to a URL string.
my $ua = LWP::UserAgent->new();
$ua->default_header("HTTP_REFERER" => "your website");
my $body = $ua->get($url);
# process the json string
my $json = from_json($body->decoded_content);
# have some fun with the results
my $i = 0;
foreach my $result (@{$json->{responseData}->{results}}){
$i++;
print $i.". " . $result->{titleNoFormatting} . "(" . $result->{url} . ")\n";
# etc....
}
if(!$i){
print "Sorry, but there were no results.\n";
}
On Fri, Sep 3, 2010 at 2:23 PM, Waverley @ Palo Alto
<waverley.paloalto at gmail.com> wrote:
> Hi,
>
> Can someone help as how to use R to program google search in the R
> code? I know that other languages can allow or have the google search
> API
>
> If someone can give me some links or sample code I would greatly appreciate.
>
> Thanks.
>
> --
> Waverley @ Palo Alto
>
--
Waverley @ Palo Alto
More information about the R-help
mailing list