【发布时间】:2020-06-06 16:40:07
【问题描述】:
我知道如何找到所有没有子节点的节点:
library(rvest)
library(magrittr)
doc <- "https://www.r-bloggers.com/" %>% GET %>% content
leafes <- doc %>% html_nodes(xpath = "//*[not(descendant::*)]")
length(leafes)
现在我从非根节点的节点尝试相同的操作:
doc <- "https://www.r-bloggers.com/" %>% GET %>% content
tags <- doc %>% html_nodes(xpath = "/html/body/div/div/div/div/h2/a")
nonRootNodeWithChildr <- tags %>% html_nodes(xpath = "..") %>% html_nodes(xpath = "..")
nonRootNodeWithChildr %>% html_nodes(xpath = "*[not(descendant::*)]")
我假设我必须使用 *[] 而不是 //* 以避免从文档根目录开始,但是,
"//" 可以确保我也可以选择 Grand++ 孩子。
【问题讨论】: