認識 position() 函式
正如你在影片中看到的,position() 函式在述詞(predicate)中非常強大。搭配運算子後,你基本上可以從符合某條路徑的節點中選出任意一個。
你會用透過 rules_html 提供給你的下列 HTML 範例來練習。我們假設這是一個持續更新的網站,用來顯示某一天與隔天的特定防疫規定。
...
<div>
<h2>Today's rules</h2>
<p>Wear a mask</p>
<p>Wash your hands</p>
</div>
<div>
<h2>Tomorrow's rules</h2>
<p>Wear a mask</p>
<p>Wash your hands</p>
<small>Bring hand sanitizer with you</small>
</div>
...
本練習屬於課程
R 的網頁爬蟲
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Select the text of the second p in every div
rules_html %>%
html_elements(xpath = ___) %>%
___