【问题标题】:Problem Loop Web Scraping With Calculation and Binding to Build Table R使用计算和绑定构建表 R 的问题循环 Web 抓取
【发布时间】:2019-01-17 14:47:09
【问题描述】:

我正在尝试使用数字股票代码构建一个网络抓取数据表,该查询通过从主机网站上为特定基金提取 1 年的价格和折扣数据的查询。

我的查询页面是正确的,但是我试图一个一个地执行的循环获取每个公司的价格历史,然后用它做几个非常基本的计算,然后将结果索引到相应的股票代码,然后绑定每个连续基金的业绩汇总成一个更大的表,让我挂了。

如果有人能找出问题并提出解决方案,这是我的示例脚本:

library(jsonlite)
library(rvest)
library(dplyr)
library(stringr)
library(PerformanceAnalytics)
library(lubridate)

tickers2 <- c("PMX", "MFM", "CEF", "JLS","CXE","BHV")
tickers2 <- paste0("https://www.cefconnect.com/api/v3/pricinghistory/",tickers2,"/1Y")

lst_scraped_data <- lapply(tickers2, FUN=function(URLLink){
        url <-URLLink 
        page<-html_session(url)
        json3<-readBin(page$response$content, what="json")
        df15 <-fromJSON(json3)
        df15 <- data.frame(df15)

        #  Attempt to Manually Calculate the first tables 52 Week Values
        test4 <- xts(df15[2:4],mdy(df15$Data.PriceHistory.DataDateDisplay))
        colnames(test4) <- c("NAV Price","Discount %","Share Price")
        obs <- dim(test4)[1]

        cur <- tail(test4,n=1)

        WeekMean <- tail(apply(test4, 2, function(x){apply.rolling(x, FUN="mean", width=dim(test4)[1])}),n=1)
        WeekMean <- data.frame(round(WeekMean,digits=2))

        WeekMin <- tail(apply(test4, 2, function(x){apply.rolling(x, FUN="min", width=dim(test4)[1])}),n=1)
        WeekMin <- data.frame(round(WeekMin,digits=2))

        WeekMax <- tail(apply(test4, 2, function(x){apply.rolling(x, FUN="max", width=dim(test4)[1])}),n=1)
        WeekMax <- data.frame(round(WeekMax,digits=2))

        complete <- data.frame(rbind(cur,WeekMean,WeekMax,WeekMin))
        row.names(complete) <- c("Current","Year Avg","Year High","Year Low")

        complete2 <- data.frame(cbind(complete[,3],complete[,1],complete[,2]))
        colnames(complete2) <- c('Share_Price',"NAV","Premium/Discount_%")
        rownames(complete2)[1] <- "Current"

        Ticker <- str_replace_all(URLLink,pattern="https://www.cefconnect.com/api/v3/pricinghistory/",replacement = "")
        Ticker <- str_replace_all(URLLink,pattern="/1Y",replacement = "")
        Checker = data.frame(df15,Ticker)    
})


df13 <- do.call(rbind, lst_scraped_data) 

理想情况下,每个股票代码的最终结果应该是这样的:

         Share_Price   NAV Premium/Discount_%  Ticker
Current        11.52 10.45              10.24  PMX
WeekMean       11.32 10.66               6.19  PMX
WeekMax        11.78 10.95              11.33  PMX
WeekMin        10.81 10.35               0.65  PMX

完成的表格将包含为循环中的每个代码绑定上面的输出。

【问题讨论】:

  • 更大的桌子把我挂了 ...这对我们没有帮助。请描述错误和/或不良结果。
  • 这是我得到的错误: (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : 参数暗示不同的行数: 1, 251, 0. 这是用完整的股票代码列表运行上述函数. 基本上, 我想查询一年的数据价值运行这些基本计算, 组织它, 为它对应的股票添加一个标识符, 和然后重复该过程。每次该过程运行时,结果都会添加到一个更大的表中,这是最终输出。感谢您的审查!
  • 例如从 url 到 rownames(complete2)[1] 执行脚本没有问题。我将 URL 替换为一个代码的静态 url,但我想要的循环会一个一个地处理更大的列表。

标签: r loops web-scraping


【解决方案1】:

目前还不清楚到底是什么问题,但也许这会有所帮助。

数据包含格式正确的日期,然后使用format 可以为我们提供给定日期的周数。

df15 <-fromJSON(json3)
df <- df15$Data$PriceHistory                # (json3 as in your function)
df$week <- as.integer(format(as.Date(df$DataDate), '%V'))

从现在开始,获取每周数据变得更加简单

# For Example NAV and Discount weekly means, maxs, mins
means <- aggregate(df[, c("NAVData", "DiscountData")], list(df$week), mean)
maxs  <- aggregate(df[, c("NAVData", "DiscountData")], list(df$week), max)
mins  <- aggregate(df[, c("NAVData", "DiscountData")], list(df$week), min)
setNames(merge(merge(means, mins, by = 'Group.1'), maxs, by = 'Group.1'), 
         c('week','NAVMean','DiscountMean','NAVMins','DiscountMins','NAVMaxs','DiscountMaxs'))
#    week NAVMean DiscountMean NAVMins DiscountMins NAVMaxs DiscountMaxs
# 1     1 10.5350       8.2575   10.49         6.20   10.57         9.89
# 2     2 10.5080       9.8980   10.46         8.71   10.56        11.33
# 3     3 10.6540       7.4200   10.45         4.75   10.95        10.24
# ...
# 50   50 10.5300       8.3500   10.49         5.34   10.59         9.92
# 51   51 10.4340       5.5520   10.38         4.39   10.48         6.56
# 52   52 10.4825       6.7975   10.48         6.20   10.49         7.82

【讨论】:

  • 感谢您的帮助。也许这不清楚,但我不是在寻找每周的 Min/Max/Avg,我正在尝试构建一个循环来收集该数据表,然后计算每个代码的 52w Min/Max/Average 和 Current 值。从那里表格将与其对应的代码配对,然后该过程将重复自身,每个连续代码的数据都被添加到整个表格中。任何想法如何做到这一点?感谢您的帮助。
猜你喜欢
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2010-10-18
相关资源
最近更新 更多