并非所有同级元素都一样
在视频中,您学习了相邻同级选择器和一般同级选择器(+ 和 ~)。
下面的 HTML 代码包含两个标题,后面依次是一些 code 和 span 标签:
<html>
<body>
<h2 class = 'first'>First example:</h2>
<code>some = code(2)</code>
<span>will compile to...</span>
<code>some = more_code()</code>
<h2 class = 'second'>Second example:</h2>
<code>another = code(3)</code>
<span>will compile to...</span>
<code>another = more_code()</code>
</body>
</html>
请注意,这里的两个代码示例并没有形成层级结构。唯一明显的差别是每个示例前面的 h2 元素的类名不同。
该 HTML 片段已为您读入,并存储在变量 code_html 中。
本练习是课程的一部分
R 语言网页抓取
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Select only the first code element in the second example
code_html %>%
___('h2.second ___ ___')