html_encoding_guess()
幫助您處理聲明錯誤編碼的網頁。使用 html_encoding_guess()
生成可能的編碼列表,然後使用 read_html()
的 encoding
參數嘗試每種編碼。 html_encoding_guess()
替換已棄用的 guess_encoding()
。
例子
# A file with bad encoding included in the package
path <- system.file("html-ex", "bad-encoding.html", package = "rvest")
x <- read_html(path)
x %>% html_elements("p") %>% html_text()
#> [1] "\xc9migré cause célèbre déjà vu."
html_encoding_guess(x)
#> encoding language confidence
#> 1 ISO-8859-1 fr 0.31
#> 2 ISO-8859-2 ro 0.22
#> 3 UTF-16BE 0.10
#> 4 UTF-16LE 0.10
#> 5 GB18030 zh 0.10
#> 6 Big5 zh 0.10
#> 7 ISO-8859-9 tr 0.06
#> 8 IBM424_rtl he 0.01
#> 9 IBM424_ltr he 0.01
# Two valid encodings, only one of which is correct
read_html(path, encoding = "ISO-8859-1") %>% html_elements("p") %>% html_text()
#> [1] "Émigré cause célèbre déjà vu."
read_html(path, encoding = "ISO-8859-2") %>% html_elements("p") %>% html_text()
#> [1] "Émigré cause célčbre déjŕ vu."
相關用法
- R rvest html_element 從 HTML 文檔中選擇元素
- R rvest html_text 獲取元素文本
- R rvest html_form 解析表單並設置值
- R rvest html_children 獲取元素子元素
- R rvest html_name 獲取元素名稱
- R rvest html_table 將 html 表解析為 DataFrame
- R rvest html_attr 獲取元素屬性
- R rvest session 在網絡瀏覽器中模擬會話
- R rvest minimal_html 從內聯 HTML 創建 HTML 文檔
- R predict.rpart 根據擬合的 Rpart 對象進行預測
- R SparkR randomSplit用法及代碼示例
- R reprex un-reprex 取消渲染reprex
- R SparkR read.stream用法及代碼示例
- R SparkR rbind用法及代碼示例
- R readr datasource 創建源對象。
- R readr melt_delim 返回分隔文件中每個標記的熔化數據(包括 csv 和 tsv)
- R readr read_rds 讀/寫 RDS 文件。
- R readr read_lines 從文件中讀取/寫入行
- R SparkR rollup用法及代碼示例
- R readr parse_number 靈活地解析數字
- R snip.rpart 剪切 Rpart 對象的子樹
- R labels.rpart 為 Rpart 對象創建分割標簽
- R SparkR refreshByPath用法及代碼示例
- R summary.rpart 總結擬合的 Rpart 對象
- R printcp 顯示擬合 Rpart 對象的 CP 表
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Guess faulty character encoding。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。