从网站解析 10 年期联邦票据收​​益率 [英] Parsing 10-year federal note yield from the website

查看:46
本文介绍了从网站解析 10 年期联邦票据收​​益率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,这是 R 中通常建议的下载 10 年期联邦票据收​​益率的方式

Typically, this is the commonly advised way in R to download the 10-year federal note yield

library(quantmod)
getSymbols("DGS10",src='FRED')
Fed<-last(DGS10)

问题是检索到的值比财务网站上提供的值早一天https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield

The problem is that the value retrieved is one day older than what is available on the treasury website https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield

有没有办法解析上面的网页来检索最近的 10 年国库券收益率?

Is there a way to parse the above webpage to retrieve the most recent 10 yr treasury note yield?

推荐答案

rvest 是一个不错的选择,因为这些是呈现表.另一个是:

rvest is a good option, as these are rendered tables. Another is:

library(httr)
library(XML)

year = 2016
URL = paste0("https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldYear&year=",year)

urldata <- httr::GET(URL)
data <- XML::readHTMLTable(rawToChar(urldata$content),
                      stringsAsFactors = FALSE)

# Finds html based list element w/daily data for the year
namesCheck <- c("Date","1 mo","3 mo","6 mo","1 yr","2 yr","3 yr","5 yr","7 yr","10 yr","20 yr","30 yr")
dataCheck <- NULL
for(i in 1:length(data)){
      dataCheck[[i]] <- names(data[[i]])
}

## Returns appropriate frame
dataCheck <- which(unlist(lapply(1:length(dataCheck), function(i) 
   (dataCheck[[i]] == namesCheck)[1])) == TRUE)

data <- as.data.frame((data[dataCheck]))
names(data) <- gsub("NULL.","", names(data)) # Take out "NULL."

tail(data)

#         Date 1.mo 3.mo 6.mo 1.yr 2.yr 3.yr 5.yr 7.yr 10.yr 20.yr 30.yr
# 245 12/22/16 0.42 0.51 0.65 0.87 1.22 1.54 2.04 2.36  2.55  2.86  3.12
# 246 12/23/16 0.42 0.52 0.65 0.87 1.22 1.54 2.04 2.35  2.55  2.86  3.12
# 247 12/27/16 0.50 0.51 0.66 0.89 1.28 1.58 2.07 2.37  2.57  2.88  3.14
# 248 12/28/16 0.48 0.53 0.62 0.90 1.26 1.55 2.02 2.32  2.51  2.83  3.09
# 249 12/29/16 0.39 0.47 0.62 0.85 1.22 1.49 1.96 2.30  2.49  2.82  3.08
# 250 12/30/16 0.44 0.51 0.62 0.85 1.20 1.47 1.93 2.25  2.45  2.79  3.06

这篇关于从网站解析 10 年期联邦票据收​​益率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆