[R] Extracting results from Google Search

Duncan Temple Lang dtemplelang at ucdavis.edu
Wed Oct 24 03:58:20 CEST 2012


Hi Eduardo

 Scraping the coordinates from the HTML page can be a little tricky
in this case.  Also, Google may not want you using their search engine
for that. Instead, you might use their Geocoding API
(https://developers.google.com/maps/documentation/geocoding),
but do ensure that this fits within their terms of use.

If you do use the Geocoding API, you can do with the following code:

library(RJSONIO)
library(RCurl)

DB<-data.frame(town=c('Ingall', 'Dogondoutchi', 'Tera'),
               country=rep('Niger',3))

location = with(DB, paste(town, country))

ans = lapply(location,
              function(loc)
                 fromJSON(getForm("http://maps.googleapis.com/maps/api/geocode/json",
                              address = loc, sensor = "false"))$results[[1]]$geometry$location
             )

DB = cbind(DB, do.call(rbind, ans))

And now the data frame has the lat and lng variables.

Again, check that the Geocoding terms of use allows you to do this.

 HTH
   D.




On 10/23/12 6:33 AM, ECAMF wrote:
> Dear list,
> 
> I have a long list of towns in Africa and would need to get their
> geographical coordinates. The Google query [/TownName Country coordinates/]
> works for most of the TownNames I have and give a nicely formatted Google
> output (try Ingall Niger coordinates for an example). I would like to launch
> a loop on the list of names I have and automatically extract the coordinates
> given by Google. Does anyone knows how it can be done?
> 
> ex.
> DB<-data.frame(town=c('Ingall', 'Dogondoutchi', 'Tera'),
> country=rep('Niger',3))
> # Get lat and lon from the Google search on : 
> for (i in 1:3)   {
>       paste(DB$town[i], DB$country[i], 'coordinates',  sep="")
> }
> 
> Many thanks!
> 
> Eduardo.
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Extracting-results-from-Google-Search-tp4647136.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>




More information about the R-help mailing list