【问题标题】:How to select all nodes which have no attributes using rvest?如何使用 rvest 选择所有没有属性的节点?
【发布时间】:2019-11-08 17:11:56
【问题描述】:

使用rvest,如何选择没有属性的节点?

例如:

<nodes>
    <node attribute1="aaaa"></node>
    <node attribute1="bbbb"></node>
    <node></node> <- FIND THIS
</nodes>

这是一个使用 XPath 的相关 thread,但是当我尝试使用类似于

rvest
wp %>% html_read(.) %>% html_nodes(xpath = "//node[not(@*)")

wp 是所需的网址,我出错了:

Warning message:
In xpath_search(x$node, x$doc, xpath = xpath, nsMap = ns, num_results = Inf) :
  Invalid predicate [1206]

当我可以看到我想要抓取的内容在页面源中没有属性时。

坦率地说,我只是不太了解 Web 开发和 HTML,无法理解如何将此示例推广到 rvest 的文档。任何帮助或资源将不胜感激!

编辑:

rvest 中实现此目的的正确代码是

wp %>% html_read(.) %>% html_nodes(xpath = "//node[not(@*)]")

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    看起来您只是缺少一个右方括号:

    library(rvest)
    
    "<nodes>
        <node attribute1=\"aaaa\" attribute2=\"cccc\"></node>
        <node attribute1=\"bbbb\"></node>
        <node></node>
    </nodes>" %>% 
      read_html() %>% 
      html_nodes(xpath = "//node[not(@*)]")
    

    给予

    {xml_nodeset (1)}
    [1] <node></node>
    

    【讨论】:

    • 很抱歉浪费了您的时间!抛出的错误让我认为这是一个逻辑错误。太感谢了。感谢您让我知道这只是语法错误
    猜你喜欢
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2020-05-30
    • 2013-10-11
    相关资源
    最近更新 更多