R for zip code and other geodata from IP addresses?

I have a list of 200,000 IP addresses. I would like to associate them with the geographical location and get any other data that the IP address can give.

The best I've found so far is the service provided by infochimps: http://www.infochimps.com/datasets/digital-element-ip-intelligence-demographics There is also an R package for info circuits. But Infochimps requires you to pay, and for 200,000 IP addresses this can become expensive.

Is there any R package that can do something like this?

thanks

+7
source share
4 answers

Try using the RDSTK package, which provides the R API API Data Science Toolkit. Below is a presentation by the author of the package, which should help you get started.

From the comments of Xu Wang (moved here to increase future search capability): For reference purposes: To install this package, you need to install RCurl and rjson. Before installing RCurl on Ubuntu, I had to install two packages: sudo apt-get install curl libcurl4-gnutls-dev I need an ip2coordinates function that takes an IP address as input

+6
source

The IPtoXY function ( http://thebiobucket.blogspot.com/2011/12/function-to-collect-geographic.html ) uses the same API, but does not need additional packages.

Edit, September 26th : Thanks to @Peter M, I became aware that my function mentioned above no longer works - here is an edited version that should work (also the link was updated ..):

 # Purpose: Get geographic coordinates for a given IP-address # Author: Kay Cichini # Date: 2011-12-18 # Output: A string holding longitude and latitude with format "X;Y" IPtoXY <- function(x) { URL_IP <- paste("http://www.datasciencetoolkit.org//ip2coordinates/", x, sep = "") api_return <- readLines(URL_IP, warn = F) lon1 <- api_return[grep("longitude", api_return)] lon <- gsub("[^[:digit:].]", "", lon1) lat1 <- api_return[grep("latitude", api_return)] lat <- gsub("[^[:digit:].]", "", lat1) return(paste(lat, lon, sep = ";")) } # Example: > IPtoXY("74.88.200.52") [1] "40.951301574707;73.78759765625" 
+2
source

The function from http://thebiobucket.blogspot.com/2011/12/function-to-collect-geographic.html does not work.

But the idea still does, so this should do:

 iplocation <- function(ip=""){ response <- readLines(paste("http://www.datasciencetoolkit.org//ip2coordinates/",ip,sep="")) success <- !any(grepl("null",response)) ip <- grep("[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*",response,value=T) match <- regexpr("[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*\\.[[:digit:]]*",ip) ip <- substr(ip,match,as.integer(attributes(match)[1])+match-1) if(success==T){ extract <- function(label,response){ text <- grep(label,response,value=T) match <- regexpr(paste('"',label,'"',": ",sep=""),text) text <- substr(text,match+as.integer(attributes(match)[1]),nchar(text)) if(grepl("[[:digit:]]",text)){ text <- substr(text,1,nchar(text)-2) }else{ text <- substr(text,2,nchar(text)-2) } if( regexpr('"',text)!= -1){ text<-substr(text,2,nchar(text)) } print(text) text } } RESULT <- list() RESULT$success <- success RESULT$ip <- ip if(success==T){ RESULT$latitude <- as.numeric(extract("latitude",response)) RESULT$longitude <- as.numeric(extract("longitude",response)) RESULT$country <- extract("country_name",response) RESULT$locality <- extract("locality",response) RESULT$postalcode <- extract("postal_code",response) RESULT$region <- extract("region",response) RESULT$countrycode <- extract("country_code3",response) } RESULT } iplocation() 
+1
source

I recently looked at ipinfo.io to find IP addresses. I just used the RCurl library to process this data:

 R> library(RCurl) R> getURL("http://ipinfo.io/74.125.227.224") [1] "{\n \"ip\": \"74.125.227.224\",\n \"hostname\": \"dfw06s38-in-f0.1e100.net\",\n \"city\": \"Mountain View\",\n \"region\": \"California\",\n \"country\": \"US\",\n \"loc\": \"37.4192,-122.0574\",\n \"org\": \"AS15169 Google Inc.\",\n \"postal\": \"94043\"\n}" 

Requests can be changed if they are only interested in the postal code, for example:

 R> getURL("http://ipinfo.io/74.125.227.224/postal") [1] "94043\n" 
+1
source

All Articles