次の構文を使用して、George Clooney のウィキペディア ページから職業情報を取得しようとしています。最終的には、さまざまなパーソナリティの職業に関するデータを取得するためのループが必要です。
ただし、以下のコードを実行すると次の問題が発生します。
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) } )