【发布时间】:2015-12-15 21:24:05
【问题描述】:
我正在运行这个函数:
require(XML)
require(plyr)
getKeyStats_xpath <- function(symbol) {
yahoo.URL <- "http://finance.yahoo.com/q/ks?s="
html_text <- htmlParse(paste(yahoo.URL, symbol, sep = ""), encoding="UTF-8")
#search for <td> nodes anywhere that have class 'yfnc_tablehead1'
nodes <- getNodeSet(html_text, "/*//td[@class='yfnc_tablehead1']")
if(length(nodes) > 0 ) {
measures <- sapply(nodes, xmlValue)
#Clean up the column name
measures <- gsub(" *[0-9]*:", "", gsub(" \\(.*?\\)[0-9]*:","", measures))
#Remove dups
dups <- which(duplicated(measures))
#print(dups)
for(i in 1:length(dups))
measures[dups[i]] = paste(measures[dups[i]], i, sep=" ")
#use siblings function to get value
values <- sapply(nodes, function(x) xmlValue(getSibling(x)))
df <- data.frame(t(values))
colnames(df) <- measures
return(df)
} else {
break
}
}
只要页面存在,它就可以正常工作。但是,如果我的其中一个代码在该 URL 上没有任何数据,则会引发错误:
Error in FUN(X[[3L]], ...) : no loop for break/next, jumping to top level
我也添加了一条踪迹,但在代码 3 上出现故障。
tickers <- c("QLTI",
"RARE",
"RCPT",
"RDUS",
"REGN",
"RGEN",
"RGLS")
tryCatch({
stats <- ldply(tickers, getKeyStats_xpath)
}, finally={})
我想这样调用函数:
stats <- ldply(tickers, getKeyStats_xpath)
rownames(stats) <- tickers
write.csv(t(stats), "FinancialStats_updated.csv",row.names=TRUE)
基本上,如果代码没有数据,我想跳过它。
有人可以帮我解决这个问题吗?
【问题讨论】:
-
为
getKeyStats_xpath编写一个包装器,将其包含在tryCatch中。您可以在ldply内使用匿名函数执行此操作,例如ldply(tickers, function (t) tryCatch(getKeyStats_xpath(t), finally={}))