有兩種方法可以從元素中檢索文本: html_text()
和 html_text2()
。 html_text()
是 xml2::xml_text()
的薄包裝,它僅返回原始底層文本。 html_text2()
使用受 JavaScript innerText() 啟發的方法來模擬文本在瀏覽器中的外觀。粗略地說,它將 <br />
轉換為 "\n"
,在 <p>
標記周圍添加空行,並稍微格式化表格數據。
html_text2()
通常是您想要的,但它比 html_text()
慢得多,因此對於性能很重要的簡單應用程序,您可能需要使用 html_text()
代替。
參數
- x
-
文檔、節點或節點集。
- trim
-
如果
TRUE
將修剪前導和尾隨空格。 - preserve_nbsp
-
是否應該保留不間斷空格?默認情況下,
html_text2()
轉換為普通空間以方便進一步計算。當preserve_nbsp
為TRUE
時,
將在字符串中顯示為"\ua0"
。這通常會引起混亂,因為它的打印方式與" "
相同。
例子
# To understand the difference between html_text() and html_text2()
# take the following html:
html <- minimal_html(
"<p>This is a paragraph.
This another sentence.<br>This should start on a new line"
)
# html_text() returns the raw underlying text, which includes whitespace
# that would be ignored by a browser, and ignores the <br>
html %>% html_element("p") %>% html_text() %>% writeLines()
#> This is a paragraph.
#> This another sentence.This should start on a new line
# html_text2() simulates what a browser would display. Non-significant
# whitespace is collapsed, and <br> is turned into a line break
html %>% html_element("p") %>% html_text2() %>% writeLines()
#> This is a paragraph. This another sentence.
#> This should start on a new line
# By default, html_text2() also converts non-breaking spaces to regular
# spaces:
html <- minimal_html("<p>x y</p>")
x1 <- html %>% html_element("p") %>% html_text()
x2 <- html %>% html_element("p") %>% html_text2()
# When printed, non-breaking spaces look exactly like regular spaces
x1
#> [1] "x y"
x2
#> [1] "x y"
# But aren't actually the same:
x1 == x2
#> [1] FALSE
# Which you can confirm by looking at their underlying binary
# representaion:
charToRaw(x1)
#> [1] 78 c2 a0 79
charToRaw(x2)
#> [1] 78 20 79
相關用法
- R rvest html_table 將 html 表解析為 DataFrame
- R rvest html_encoding_guess 猜測字符編碼錯誤
- R rvest html_element 從 HTML 文檔中選擇元素
- R rvest html_form 解析表單並設置值
- R rvest html_children 獲取元素子元素
- R rvest html_name 獲取元素名稱
- 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等大神的英文原創作品 Get element text。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。