【问题标题】:How do i find all nodes without children (starting from non-root node!) in xpath/R?如何在 xpath/R 中找到所有没有子节点的节点(从非根节点开始!)?
【发布时间】: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++ 孩子。

【问题讨论】:

    标签: r xpath rvest


    【解决方案1】:

    我认为

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

    可能是我想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多