如何在 xpath/R 中找到所有没有子节点的节点(从非根节点开始!)? [英] How do i find all nodes without children (starting from non-root node!) in xpath/R?

查看:39
本文介绍了如何在 xpath/R 中找到所有没有子节点的节点(从非根节点开始!)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何找到所有没有子节点的节点:

库(rvest)图书馆(magrittr)doc <- "https://www.r-bloggers.com/" %>% GET %>% 内容叶子 <- doc %>% html_nodes(xpath = "///*[not(descendant::*)]")长度(叶子)

现在我从不是根节点的节点尝试相同的方法:

doc <- "https://www.r-bloggers.com/" %>% GET %>% 内容标签 <- 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++ 儿童.

解决方案

我认为

nonRootNodeWithChildr %>% html_nodes(xpath = ".//*[not(descendant::*)]")

可能是我要找的.

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.

解决方案

I think

nonRootNodeWithChildr %>% html_nodes(xpath = ".//*[not(descendant::*)]")

could be what i was looking for.

这篇关于如何在 xpath/R 中找到所有没有子节点的节点(从非根节点开始!)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆