了解 position() 函数
正如您在视频中看到的,position() 函数在谓词中使用时非常强大。配合运算符,您基本可以从匹配某一路径的节点中选择任意一个。
您将使用通过 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 = ___) %>%
___