问题
I know how to find all nodes that dont have a child node:
library(rvest)
library(magrittr)
doc <- "https://www.r-bloggers.com/" %>% GET %>% content
leafes <- doc %>% html_nodes(xpath = "//*[not(descendant::*)]")
length(leafes)
Now i try the same from nodes that are not the root node:
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::*)]")
I assume that i have to go for *[]
instead of //*
to avoid starting at document root, however,
"//" would assure i can also go for the Grand++ children.
回答1:
I think
nonRootNodeWithChildr %>% html_nodes(xpath = ".//*[not(descendant::*)]")
could be what i was looking for.
来源:https://stackoverflow.com/questions/60352392/how-do-i-find-all-nodes-without-children-starting-from-non-root-node-in-xpath