html_element()
和 html_elements()
使用 CSS 選擇器或 XPath 表達式查找 HTML 元素。 CSS 選擇器與 https://selectorgadget.com/ 結合使用特別有用,這使得您可以輕鬆找到所需的選擇器。
CSS 選擇器支持
CSS 選擇器通過以下方式轉換為 XPath 選擇器選擇器package,這是python的一個端口CSS選擇 Library ,https://pythonhosted.org/cssselect/.
它實現了大多數 CSS3 選擇器,如中所述https://www.w3.org/TR/2011/REC-css3-selectors-20110929/。下麵列出了例外情況:
-
需要交互性的偽選擇器將被忽略:
:hover
、:active
、:focus
、:target
、:visited
。 -
以下偽類不適用於通配符元素 *:
*:first-of-type
、*:last-of-type
、*:nth-of-type
、*:nth-last-of-type
、*:only-of-type
-
它支持
:contains(text)
-
可以使用!=,
[foo!=bar]
與:not([foo=bar])
相同 -
:not()
接受一係列簡單選擇器,而不僅僅是一個簡單選擇器。
例子
html <- minimal_html("
<h1>This is a heading</h1>
<p id='first'>This is a paragraph</p>
<p class='important'>This is an important paragraph</p>
")
html %>% html_element("h1")
#> {html_node}
#> <h1>
html %>% html_elements("p")
#> {xml_nodeset (2)}
#> [1] <p id="first">This is a paragraph</p>
#> [2] <p class="important">This is an important paragraph</p>
html %>% html_elements(".important")
#> {xml_nodeset (1)}
#> [1] <p class="important">This is an important paragraph</p>
html %>% html_elements("#first")
#> {xml_nodeset (1)}
#> [1] <p id="first">This is a paragraph</p>
# html_element() vs html_elements() --------------------------------------
html <- minimal_html("
<ul>
<li><b>C-3PO</b> is a <i>droid</i> that weighs <span class='weight'>167 kg</span></li>
<li><b>R2-D2</b> is a <i>droid</i> that weighs <span class='weight'>96 kg</span></li>
<li><b>Yoda</b> weighs <span class='weight'>66 kg</span></li>
<li><b>R4-P17</b> is a <i>droid</i></li>
</ul>
")
li <- html %>% html_elements("li")
# When applied to a node set, html_elements() returns all matching elements
# beneath any of the inputs, flattening results into a new node set.
li %>% html_elements("i")
#> {xml_nodeset (3)}
#> [1] <i>droid</i>
#> [2] <i>droid</i>
#> [3] <i>droid</i>
# When applied to a node set, html_element() always returns a vector the
# same length as the input, using a "missing" element where needed.
li %>% html_element("i")
#> {xml_nodeset (4)}
#> [1] <i>droid</i>
#> [2] <i>droid</i>
#> [3] <NA>
#> [4] <i>droid</i>
# and html_text() and html_attr() will return NA
li %>% html_element("i") %>% html_text2()
#> [1] "droid" "droid" NA "droid"
li %>% html_element("span") %>% html_attr("class")
#> [1] "weight" "weight" "weight" NA
相關用法
- R rvest html_encoding_guess 猜測字符編碼錯誤
- 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等大神的英文原創作品 Select elements from an HTML document。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。