How do i find all nodes without children (starting from non-root node!) in xpath/R?

女生的网名这么多〃 提交于 2020-04-30 06:24:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!