html_attr()
獲取單個屬性; html_attrs()
獲取所有屬性。
參數
- x
-
文檔(來自
read_html()
)、節點集(來自html_elements()
)、節點(來自html_element()
)或會話(來自session()
)。 - name
-
要檢索的屬性的名稱。
- default
-
當該屬性不存在於每個元素中時用作默認值的字符串。
例子
html <- minimal_html('<ul>
<li><a href="https://a.com" class="important">a</a></li>
<li class="active"><a href="https://c.com">b</a></li>
<li><a href="https://c.com">b</a></li>
</ul>')
html %>% html_elements("a") %>% html_attrs()
#> [[1]]
#> href class
#> "https://a.com" "important"
#>
#> [[2]]
#> href
#> "https://c.com"
#>
#> [[3]]
#> href
#> "https://c.com"
#>
html %>% html_elements("a") %>% html_attr("href")
#> [1] "https://a.com" "https://c.com" "https://c.com"
html %>% html_elements("li") %>% html_attr("class")
#> [1] NA "active" NA
html %>% html_elements("li") %>% html_attr("class", default = "inactive")
#> [1] "inactive" "active" "inactive"
相關用法
- R rvest html_encoding_guess 猜測字符編碼錯誤
- R rvest html_text 獲取元素文本
- R rvest html_element 從 HTML 文檔中選擇元素
- R rvest html_form 解析表單並設置值
- R rvest html_children 獲取元素子元素
- R rvest html_name 獲取元素名稱
- R rvest html_table 將 html 表解析為 DataFrame
- 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 attributes。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。