我正在尝试使用以下语法从George Clooney的维基百科页面获取职业信息.最终我希望有一个循环来获取各种人格职业的数据.
但是,运行以下代码时出现以下问题:
Run Code Online (Sandbox Code Playgroud)Error in if (symbol != "role") symbol = NULL : argument is of length zero
我不确定为什么会继续这样.
library(XML)
library(plyr)
  url = 'http://en.wikipedia.org/wiki/George_Clooney'  
# don't forget to parse the HTML, doh!
  doc = htmlParse(url)  
# get every link in a table cell:
  links = getNodeSet(doc, '//table/tr/td') 
# make a data.frame for each node with non-blank text, link, and 'title' attribute:
  df = ldply(links, function(x) {
                text = xmlValue(x)
            if (text=='') text=NULL
         symbol = xmlGetAttr(x, 'class')
         if (symbol!='role') symbol=NULL
         if(!is.null(text) & !is.null(symbol))
                 data.frame(symbol, text)         } )  
Ric*_*ton 29
作为@gsee提到的,你需要检查symbol是不是NULL你检查它的价值了.这是对您的代码的一个小更新(至少对于George来说).
df = ldply(
  links, 
  function(x) 
  {
    text = xmlValue(x)
    if (!nzchar(text)) text = NULL
    symbol = xmlGetAttr(x, 'class')
    if (!is.null(symbol) && symbol != 'role') symbol = NULL
    if(!is.null(text) & !is.null(symbol))
      data.frame(symbol, text)         
  } 
)
| 归档时间: | 
 | 
| 查看次数: | 77676 次 | 
| 最近记录: |