如何使用 R 查找 IP 地址信息
在本文中,我们将使用 IPinfo API 提取 IP 信息。
IPinfo 是一个免费的 API,用于获取有关特定 IP 地址的信息,例如它们的位置。我们将使用 httr GET() 发出 URL 请求并将数据存储在变量中。现在我们需要 JSON 数据,因此我们必须将数据转换为 char 格式,因为 GET() 返回原始数据,所以我们需要使用 rawToChar() 并将 char 数据转换为 JSON 格式,我们将使用内置 fromJSON() jsonlite 包。
注意:如果您已经安装了软件包,则无需在脚本中包含 install.package()。
例子:
R
# installing required packages
install.packages("httr")
install.packages("jsonlite")
# importing packages/libraries
library(httr)
library(jsonlite)
# storing url in a variable
# you can also pass the url directly in GET()
url1 = "https://ipinfo.io/161.185.160.93/geo"
# requesting raw data from URL using GET()
# and storing in data variable.
data <- GET(url1)
# converting raw data to char format.
rawdata <- rawToChar(data$content)
# converting char data to json format.
jsondata <- fromJSON(rawdata)
# printing output
# we are using cat() instead of print function
# so that we can use new line
cat(paste("IP:", jsondata$ip,"\nCITY:",
jsondata$city,"\nRegion:",
jsondata$region,"\nCOUNTRY:",
jsondata$country,"\nCOORDINATES:",
jsondata$loc))
R
# installing required packages
install.packages("httr")
install.packages("jsonlite")
# importing packages/libraries
library(httr)
library(jsonlite)
# storing incomplete url in a variable
url = "https://ipinfo.io/"
# manually taking ip as a input from user
ip = readline("Enter your IP address:")
# converting input ip address to character
ip = as.character(ip)
# completing url
url1 = paste0(url,ip,"/geo")
# requesting raw data from URL using GET()
# and string in rawdata variable.
rawdata <- GET(url1)
# converting raw data to char format.
chardata <- rawToChar(rawdata$content)
# converting char data to json format.
jsondata <- fromJSON(chardata)
# printing output
# we are using cat() instead of print function
# so that we can use new line
cat(paste("IP:", jsondata$ip,"\nCITY:",
jsondata$city,"\nRegion:",
jsondata$region,"\nCOUNTRY:",
jsondata$country,"\nCOORDINATES:",
jsondata$loc))
输出:
IP: 161.185.160.93
CITY: New York City
Region: New York
COUNTRY: US
COORDINATES: 40.7143,-74.0060
示例:从用户手动获取输入
为了从用户那里获取输入,我们将使用 readline() 并使用 as 将其转换为字符。字符()。现在我们将使用 paste0() 来完成 URL 并将其传递给 GET()函数。
R
# installing required packages
install.packages("httr")
install.packages("jsonlite")
# importing packages/libraries
library(httr)
library(jsonlite)
# storing incomplete url in a variable
url = "https://ipinfo.io/"
# manually taking ip as a input from user
ip = readline("Enter your IP address:")
# converting input ip address to character
ip = as.character(ip)
# completing url
url1 = paste0(url,ip,"/geo")
# requesting raw data from URL using GET()
# and string in rawdata variable.
rawdata <- GET(url1)
# converting raw data to char format.
chardata <- rawToChar(rawdata$content)
# converting char data to json format.
jsondata <- fromJSON(chardata)
# printing output
# we are using cat() instead of print function
# so that we can use new line
cat(paste("IP:", jsondata$ip,"\nCITY:",
jsondata$city,"\nRegion:",
jsondata$region,"\nCOUNTRY:",
jsondata$country,"\nCOORDINATES:",
jsondata$loc))
输出:
Enter your IP address:161.185.160.93
IP: 161.185.160.93
CITY: New York City
Region: New York
COUNTRY: US
COORDINATES: 40.7143,-74.0060