我有一个大约200,000个IP地址的列表。我想将这些链接到地理位置,并获取IP地址可以提供的任何其他数据。
迄今为止我发现的最好的是由infochimps提供的服务: http://www.infochimps.com/datasets/digital-element-ip-intelligence-demographics 还有一个用于infochimps的R包。但是,infochimps要求你支付200,000个IP地址,这可能会花费很多。
有没有R包可以做这样的事情?
谢谢
答案 0 :(得分:6)
尝试使用RDSTK
包,它提供了Data Science Toolkit API的R接口。这是程序包作者的presentation,可以帮助您入门。
来自Xu Wang的评论(移到这里以增加未来的可发现性):
供参考:要安装该软件包,必须安装RCurl和rjson。在安装RCurl之前,在Ubuntu上我必须安装两个软件包:sudo apt-get install curl libcurl4-gnutls-dev
我需要的函数是ip2coordinates
,它接受一个IP地址作为输入
答案 1 :(得分:2)
功能IPtoXY(http://thebiobucket.blogspot.com/2011/12/function-to-collect-geographic.html)使用相同的API,但不需要额外的包..
编辑,9月26日: 感谢@Peter M我意识到上面提到的我的功能不再起作用了 - 这是编辑后的版本应该有用(上面的链接也更新了......):
# 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 :(得分:1)
来自http://thebiobucket.blogspot.com/2011/12/function-to-collect-geographic.html的功能不起作用。
但这个想法仍然存在,所以这应该做:
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()
答案 3 :(得分:1)
我最近遇到ipinfo.io来查找IP地址。我刚刚使用RCurl库来处理这些:
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}"
如果只对邮政编码感兴趣,可以修改请求,例如:
R> getURL("http://ipinfo.io/74.125.227.224/postal")
[1] "94043\n"