2011-09-23 55 views
4

雅虎財經有data on historic analyst opinions股票。我感興趣的是拉這個數據爲R進行分析,這裏是我到目前爲止有:從雅虎財經拉動歷史分析師意見R

getOpinions <- function(symbol) { 
    require(XML) 
    require(xts) 
    yahoo.URL <- "http://finance.yahoo.com/q/ud?" 
    tables <- readHTMLTable(paste(yahoo.URL, "s=", symbol, sep = ""), stringsAsFactors=FALSE) 
    Data <- tables[[11]] 
    Data$Date <- as.Date(Data$Date,'%d-%b-%y') 
    Data <- xts(Data[,-1],order.by=Data[,1]) 
    Data 
} 

getOpinions('AAPL') 

我很擔心這個代碼將打破,如果表中的位置(目前11)的變化,但我想不出一種優雅的方式來檢測哪個表具有我想要的數據。我試過the solution posted here,但它似乎不適用於這個問題。

如果雅虎重新安排他們的網站,是否有更好的方式來刮取這種不太可能破壞的數據?

編輯:它看起來已經有一個包(fImport)在那裏做這個。

library(fImport) 
yahooBriefing("AAPL") 

這裏是他們的解決方案,它不返回XTS對象,可能會打破,如果頁面佈局的變化(在fImport的yahooKeystats功能已經損壞):

function (query, file = "tempfile", source = NULL, save = FALSE, 
    try = TRUE) 
{ 
    if (is.null(source)) 
     source = "http://finance.yahoo.com/q/ud?s=" 
    if (try) { 
     z = try(yahooBriefing(query, file, source, save, try = FALSE)) 
     if (class(z) == "try-error" || class(z) == "Error") { 
      return("No Internet Access") 
     } 
     else { 
      return(z) 
     } 
    } 
    else { 
     url = paste(source, query, sep = "") 
     download.file(url = url, destfile = file) 
     x = scan(file, what = "", sep = "\n") 
     x = x[grep("Briefing.com", x)] 
     x = gsub("</", "<", x, perl = TRUE) 
     x = gsub("/", "/", x, perl = TRUE) 
     x = gsub(" class=.yfnc_tabledata1.", "", x, perl = TRUE) 
     x = gsub(" align=.center.", "", x, perl = TRUE) 
     x = gsub(" cell.......=...", "", x, perl = TRUE) 
     x = gsub(" border=...", "", x, perl = TRUE) 
     x = gsub(" color=.red.", "", x, perl = TRUE) 
     x = gsub(" color=.green.", "", x, perl = TRUE) 
     x = gsub("<.>", "", x, perl = TRUE) 
     x = gsub("<td>", "@", x, perl = TRUE) 
     x = gsub("<..>", "", x, perl = TRUE) 
     x = gsub("<...>", "", x, perl = TRUE) 
     x = gsub("<....>", "", x, perl = TRUE) 
     x = gsub("<table>", "", x, perl = TRUE) 
     x = gsub("<td nowrap", "", x, perl = TRUE) 
     x = gsub("<td height=....", "", x, perl = TRUE) 
     x = gsub("&amp;", "&", x, perl = TRUE) 
     x = unlist(strsplit(x, ">")) 
     x = x[grep("-...-[90]", x, perl = TRUE)] 
     nX = length(x) 
     x[nX] = gsub("@$", "", x[nX], perl = TRUE) 
     x = unlist(strsplit(x, "@")) 
     x[x == ""] = "NA" 
     x = matrix(x, byrow = TRUE, ncol = 9)[, -c(2, 4, 6, 8)] 
     x[, 1] = as.character(strptime(x[, 1], format = "%d-%b-%y")) 
     colnames(x) = c("Date", "ResearchFirm", "Action", "From", 
      "To") 
     x = x[nrow(x):1, ] 
     X = as.data.frame(x) 
    } 
    X 
} 
+0

發生了什麼事yahooBriefing? – Rhodo

回答

3

這裏一個黑客,你可以使用。在您的功能中,添加以下內容

# GET THE POSITION OF TABLE WITH MAX. ROWS 
position = which.max(sapply(tables, NROW)) 
Data  = tables[[position]] 

只要頁面上最長的表格是您所尋找的,就會工作。

如果你想多一點健壯,這裏是另一種方法

# GET POSITION OF TABLE CONTAINING RESEARCH FIRM IN ITS NAMES 
position = sapply(tables, function(tab) 'Research Firm' %in% names(tab)) 
Data  = tables[position == TRUE]