在Ubuntu上使用正确的字符编码崩溃的xml2 :: read_html

时间:2017-01-04 05:47:04

标签: r ubuntu character-encoding rvest xml2

尝试使用正确的字符编码时,

xml2 :: read_html会破坏Ubuntu(但不会在Mac上)。

   library(xml2)
   library(httr) 
   # GET webpage that is encoded using Big5 (Chinese)
   pg <- GET("http://chinesenews.net.au")
   # Identify encoding using rvest package function, which returns 
   # incorrect encoding as ISO-8859-1
   enc1 <- rvest::guess_encoding(httr::content(pg, "raw"))$encoding[1]
   # Use hack to identify the right encoding using a function from stringi package
   enc2 <- as.character(
                as.data.frame(
                      stringi::stri_enc_detect(httr::content(pg, "raw"))[[1]])[1,1]) 
   # So far so good. 
   # Let's try to read_html with both encodings
   # Using ISO-8859-1 encoding, there is not problem
   ht1 <- xml2::read_html(pg, encoding=enc1) # Reads, but characters are distorted
   # However, using correct (Big5) encoding crashes on Ubuntu
   ht2 <- xml2::read_html(pg, encoding=enc2) 

错误是:

  

doc_parse_raw中的错误(x,encoding = encoding,base_url = base_url,as_html = as_html,:basic_string :: _ M_replace_aux

因为问题发生在Ubuntu上而不是Mac上,所以尝试使用

安装最新版本的xml2库
devtools::install_github("hadley/xml2")

仍有错误,但有一个错误:

  

doc_parse_raw出错(x,encoding = encoding,base_url = base_url,as_html = as_html,:由于输入错误,输入转换失败,字节0xFB 0x7C 0xB7 0x51 [6003]

我不确定为什么传递正确的编码会崩溃libxlm2。有什么想法可以做些什么?

这是我的Ubuntu sessionInfo():

R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] httr_1.2.1   xml2_1.0.0   magrittr_1.5

loaded via a namespace (and not attached):
 [1] selectr_0.3-1   R6_2.2.0        tools_3.2.3     curl_2.3       
 [5] urltools_1.6.0  Rcpp_0.12.8     triebeard_0.3.0 stringi_1.1.2  
 [9] stringr_1.1.0   rvest_0.3.2     purrr_0.2.2   

1 个答案:

答案 0 :(得分:0)

尝试使用encoding = "latin1"