避免在R中进行url编码

时间:2015-03-02 09:06:54

标签: r url encoding rcurl httr

我正在尝试从Google Geocoding API获取lat / lon,但是当丹麦语本地字符在地址中时,请求失败。我怀疑是因为httr :: GET函数对url进行编码,但我不确定我是否正确。

如果您将此链接直接复制/粘贴到浏览器中,则会获得有效结果: http://maps.googleapis.com/maps/api/geocode/json?address =Søholmen+ 9,+ 4500 +丹麦

但是下面的代码无效,即使在解析为GET函数之前url是相同的。如果我使用没有本地字符的地址,它就可以工作。

library(httr)
library(jsonlite)
library(stringr)

address <- "Søholmen 9, 4500 Denmark"
# address <- "Kronprinsesse Sofies Vej 6, 2000 Denmark"

base_url <- "http://maps.googleapis.com/maps/api/geocode/json?"

# An address OR components
geo_url <- paste0(base_url, "address=", str_replace_all(address, pattern = " ", replacement = "+"))

# Get the result
# get the content
# Parse the JSON
temp_geo_results <- httr::GET(url = URLencode(URL = geo_url), verbose())
temp_geo_results <- httr::content(temp_geo_results, as = "text")
temp_geo_results <- jsonlite::fromJSON(temp_geo_results)

这是我的sessionInfo()

R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Danish_Denmark.1252  LC_CTYPE=Danish_Denmark.1252        LC_MONETARY=Danish_Denmark.1252
[4] LC_NUMERIC=C                    LC_TIME=Danish_Denmark.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_0.6.2   jsonlite_0.9.10 httr_0.5       

loaded via a namespace (and not attached):
[1] RCurl_1.95-4.3 tools_3.1.2 

编辑:我删除了问题所不需要的一行代码,并添加了我的sessionInfo。

4 个答案:

答案 0 :(得分:4)

这似乎是一个编码问题。

以下对我来说很好:

address <- "Søholmen 9, 4500 Denmark"
u <- sprintf("http://maps.googleapis.com/maps/api/geocode/json?address=%s", 
             gsub('\\s+', '+', enc2utf8(address)))

fromJSON(content(GET(u), as='text'))

答案 1 :(得分:0)

您可以使用rvest包

library(rvest); library(jsonlite)
address <- "Søholmen 9, 4500 Denmark"
# address <- "Kronprinsesse Sofies Vej 6, 2000 Denmark"
base_url <- "http://maps.googleapis.com/maps/api/geocode/json?"

# An address OR components
geo_url <- paste0(base_url, "address=", str_replace_all(address, pattern = " ", replacement = "+"))
geo_url <- iconv(geo_url, to="UTF-8")

temp_geo_results <- html_text(html_nodes(html(geo_url) , "p"))
temp_geo_results <- fromJSON(temp_geo_results)

答案 2 :(得分:0)

我如何解决类似问题:在 EncodingrawToChar 之间设置 fromJSON,如下所示(不可执行)。

library(httr)
library(jsonlite)

call_api <- GET("YOUR_URL",
                  add_headers(.headers=c(`Authorization` = "YOUR_KEY")))

strange_characters <- rawToChar(call_api$content) #wherever the raw_data is

# if you pass Encoding(strange_characters) you will get "unknown". So run the line below.
Encoding(strange_characters) <- "UTF-8" 
  
right_characters <- fromJSON(strange_characters)

答案 3 :(得分:-1)

我可以用粗俗的方式分享我用我的语言解决同样的问题:

deencode <- function(text){
  output <- NULL
  for(i in 1:length(text)){
    temp <- text[i]
    temp <- gsub("ā", "a", temp)  
    temp <- gsub("Ā", "A", temp)
    temp <- gsub("č", "c", temp)
    temp <- gsub("Č", "C", temp)
    temp <- gsub("ē", "e", temp)
    temp <- gsub("Ē", "E", temp)
    temp <- gsub("ģ", "g", temp)
    temp <- gsub("Ģ", "G", temp)
    temp <- gsub("ī", "i", temp)
    temp <- gsub("Ī", "I", temp)
    temp <- gsub("ķ", "k", temp)
    temp <- gsub("Ķ", "K", temp)
    temp <- gsub("ļ", "l", temp)
    temp <- gsub("Ļ", "L", temp)
    temp <- gsub("ņ", "n", temp)
    temp <- gsub("Ņ", "N", temp)
    temp <- gsub("š", "s", temp)
    temp <- gsub("Š", "S", temp)
    temp <- gsub("ū", "u", temp)
    temp <- gsub("Ū", "u", temp)
    temp <- gsub("ž", "z", temp)
    temp <- gsub("Ž", "Z", temp)
    output <- c(output, temp)
  }
  return(output)
}

在这种简单的替换之后,这一切都奏效了,至少在谷歌地理编码API中是这样。