0

我有从xmlParse(). 我能够获得对名为的 XMLNode 的引用root

> class(root)
[1] "XMLInternalElementNode" "XMLInternalNode"        "XMLAbstractNode"     

当我做

> root[[2]][[1]]
<tr class="party-republican race-winner"><th rowspan="5" class="results-county">Autauga <span class="precincts-reporting">100.0% Reporting</span></th>&#13;
                                &#13;
                                <th scope="row" class="results-candidate">M. Romney</th>&#13;
                                <td class="results-party"><abbr title="Republican">GOP</abbr></td>&#13;
                                <td class="results-percentage">72.6%</td>&#13;
                                <td class="results-popular">    17,366</td>&#13;
                            </tr> 

我尝试引用标签中的值:

<td class="results-percentage">

但是做root[[1]][[2]][["<td class='results-percentage'>]]返回null

我做错了什么导致我无法访问 72.6% 的值?

4

1 回答 1

1

你应该给出一个有效的xpath,像这样的东西:

  //td[@class='results-percentage'] ## preeceeding by td and cotaining a certain class

使用您的数据:

library(XML)
dd <- xmlParse('<tr class="party-republican race-winner"><th rowspan="5" class="results-county">Autauga <span class="precincts-reporting">100.0% Reporting</span></th>&#13;
  &#13;
  <th scope="row" class="results-candidate">M. Romney</th>&#13;
  <td class="results-party"><abbr title="Republican">GOP</abbr></td>&#13;
  <td class="results-percentage">72.6%</td>&#13;
  <td class="results-popular">    17,366</td>&#13;
  </tr> ',asText=TRUE)

然后应用 xpath

getNodeSet(dd, "//td[@class='results-percentage']/text()")[[1]]
72.6% 

或使用xpathSApply

xpathSApply(dd, "//td[@class='results-percentage']",xmlValue)
[1] "72.6%"
于 2013-04-20T07:59:03.613 回答