需要帮助提取xpath-R吗

需要帮助提取xpath-R吗,r,xml,xpath,R,Xml,Xpath,我正在尝试从HTML内容生成一个表。我制作了一个示例HTML文档来准确地说明问题所在-因此,尽管有许多更简单的方法来完成我在本示例中提出的问题,但鉴于我正在处理的实际更大的HTML文档,我必须以这种方式(制作单独的向量)来完成 基本上,我需要从格式怪异的表中的顶行和底行提取特定值。但是,有时特定的行/列中没有可用的值(甚至不是伪空值),因此我无法设置表,因为变量的长度不同 例如: library(XML) library(rvest) htmlEx <- read_html( '<

我正在尝试从HTML内容生成一个表。我制作了一个示例HTML文档来准确地说明问题所在-因此,尽管有许多更简单的方法来完成我在本示例中提出的问题,但鉴于我正在处理的实际更大的HTML文档,我必须以这种方式(制作单独的向量)来完成

基本上,我需要从格式怪异的表中的顶行和底行提取特定值。但是,有时特定的行/列中没有可用的值(甚至不是伪空值),因此我无法设置表,因为变量的长度不同

例如:

library(XML)
library(rvest)
htmlEx <- read_html(
  '<table>
    <thead>
      <tbody>
        <tr class="top">
          <td class="price">
            <span class="data-value"> 150 </span>
            <small class="name"> Good1 </small>
          </td>
        </tr>
        <tr class="bottom">
          <td class="price">
            <small class="name"> Good2 </small>
          </td>
        </tr>
        <tr class="top">
          <td class="price">
            <span class="data-value"> 130 </span>
            <small class="name"> Good3 </small>
          </td>
        </tr>
        <tr class="bottom">
          <td class="price">
            <span class="data-value"> 180 </span>
            <small class="name"> Good4 </small>
          </td>
        </tr>
      </tbody>
    </thead>
  </table>'
)

htmlEx <- htmlTreeParse(htmlEx, useInternalNodes=T)

topVals <- trimws((xpathApply(htmlEx, paste('//*[contains(@class, "top")]//span', sep = ''), xmlValue)))
topNames <- trimws((xpathApply(htmlEx, paste('//*[contains(@class, "top")]//small', sep = ''), xmlValue)))

bottomVals <- trimws((xpathApply(htmlEx, paste('//*[contains(@class, "bottom")]//span', sep = ''), xmlValue)))
bottomNames <- trimws((xpathApply(htmlEx, paste('//*[contains(@class, "bottom")]//small', sep = ''), xmlValue)))
库(XML)
图书馆(rvest)

htmlEx当节点不存在时,这将用空字符串填充它:

convert_empty <- function(x) {
  value <- xpathApply(x, './span/text()')
  if (is.null(value) ){ return ('') }
  return (xmlValue(value[1]))
  }
bottomVals <- trimws((xpathApply(htmlEx, paste('//*[contains(@class, "bottom")]/td', sep = ''), convert_empty)))


convert\u empty这里有一个可能的解决方案,只使用rvest。当html/xml结构缺少一些节点时,最简单的解决方案是找到每个感兴趣的数据点所共有的节点

在这种情况下,“tr”行是常见的。从那里,using
html\u node()
函数将为每个解析节点返回一个值,即使不存在感兴趣的子节点

library(rvest)
#find all tr nodes
tablerows<- html_nodes(htmlEx, "tr") 

#parse each tr node and obtain the span value, name value and class
spanrows <- html_node(tablerows, "span") %>% html_text()
smallrows <- html_node(tablerows, "small") %>% html_text()
rowclasses <- tablerows %>% html_attr("class")

df<- data.frame(class = rowclasses, Names = spanrows, Values =smallrows)
df

   class Names Values 
1    top  150   Good1   
2 bottom  <NA>  Good2   
3    top  130   Good3   
4 bottom  180   Good4   
库(rvest)
#查找所有tr节点

tablerowsWould的表格功能帮助
html\u节点(htmlEx,“table”)%%>%html\u table()
在本例中是的,这通常是我的目标,但在我的实际问题中,DOM的设置非常奇怪,以至于
html\u table()
无法工作。我目前的方法已经使用了多年,但他们最近更新了他们的站点,以删除
元素,而不是将其保留为空白。我认为这可能是直接的,但我不了解您要查找的表的哪一行。你能澄清你想提取什么信息吗。鉴于以上示例,您希望的输出是什么?@Dave2e对此表示抱歉,已用所需结果更新了我的帖子。@CoolGuyHasChillDay。这回答了你的问题吗?
library(tidyr)
df$id = rep(1:(nrow(df)/2), each=2)
pivot_wider(df, id_cols=id, names_from=class, names_glue = "{class}_{.value}", values_from = c(Values, Names))

# A tibble: 2 x 5
id top_Values bottom_Values top_Names bottom_Names
<int> <fct>      <fct>         <fct>     <fct>       
    1 " Good1 "  " Good2 "     " 150 "    NA         
    2 " Good3 "  " Good4 "     " 130 "   " 180 "