我需要从 IP 地址列表(大约 300 个)生成国家/地区代码,并使用我的理学硕士项目的 R 代码将它们绘制在地图上。
我尝试过 rgeolocation,但在我的 R 版本 4.3.1 上不行,而且 maxmind 也不起作用。还尝试了 iptocountry 但也不起作用。我认为 ip2location 包可以工作,我安装得很好,但后来我似乎无法得到我需要的结果,因为 maxmind 与 ip2location 不兼容......我对编码游戏很陌生,并试图拼凑在一起下面,但它在使用 maxmind 提取结果时出错(显然)。我不知道替代方案是什么??..
如果有人可以提供一些建议/修复代码,我将永远感激不已!
谢谢:)你的女儿,斯蒂芬
#Tried this but it errors on the results/maxmind line
#load the data set
options(scipen = 999)
options(max.print = 1000000)
data = readr::read_csv("C:/Users/Steph/Documents/ip_address.csv")
View(data)
#Load the package
library(ip2location)
#Step 2: Convert IP Address to Country Analysis
setwd("C:/Users/Steph/Documents")
ipdf <- read.csv("C:/Users/Steph/Documents/ip_address.csv")
ipmmdb <- system.file("extdata","GeoLite2-Country.mmdb", package = "ip2location")
results <- maxmind(ipdf$IP.Address, ipmmdb,"country_name") #error here
export.results <- data.frame(ipdf$IP.Address, results$country_name)
colnames(export.results) <- c("IP Address", "Country")
write.csv(export.results, "C:/Users/Steph/Documents/IP_to_Locationmmdb.csv")
#not sure how i can plot my file data on the map ...
plot_map(ips)
# these are random IP addresses I got from https://catonmat.net/tools/generate-random-ip-addresses
df <- read.table(text="
ip_address
178.10.17.178
157.213.207.99
94.230.235.15
97.103.63.156
248.161.179.64
12.114.230.175
68.68.181.51
59.7.213.18
59.38.90.12
103.213.135.197", header = TRUE)
# load libraries
pacman::p_load(ip2location, reticulate, tidyverse)
# my computer has multiple versions of python installed, so I need to specify which one to use. reticulate uses one that doesn't have IP2Location installed, but I can't figure out which one it is lol
# replace this with your own python path if you have already installed IP2Location using pip but are still getting an error about IP2Location not being installed
use_python("/opt/homebrew/bin/python3")
# load IP2Location bin file
ip2location::open("/Users/me/Downloads/IP2LOCATION-LITE-DB11.BIN/IP2LOCATION-LITE-DB11.BIN")
df <- df |>
mutate(details = map(ip_address, get_all)) |>
unnest_wider(details)
# df
region city latitude longitude zipcode timezone
<chr> <chr> <chr> <chr> <chr> <chr>
1 Hessen Eschborn 50.143517 8.570921 65760 +02:00
2 Ohio Columbus 39.966381 -83.012772 43218 -04:00
3 Toshkent Tashkent 41.264648 69.216270 700011 +05:00
4 Florida Orlando 28.538340 -81.379242 32801 -04:00
5 - - 0.000000 0.000000 - -
6 New Jersey Piscataway 40.554516 -74.460167 08854 -04:00
7 Tennessee Camden 36.058815 -88.102158 38320 -05:00
8 Gyeonggi-do Seongnam 37.420624 127.126717 13118 +09:00
9 Guangdong Foshan 23.026770 113.131477 528000 +08:00
10 Jiangsu Changzhou 31.783331 119.966667 213019 +08:00
plot_map(df$ip_address)