从财政部网站刮取联邦票据收​​益表

时间:2017-04-16 00:41:17

标签: html r quantmod rvest httr

我想从财政部网站下载10年期联邦票据收​​益率:https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield

要解析上述网页并检索最近10年的国库券收益率,我曾按照此处提供的说明操作: Parsing 10-year federal note yield from the website

library(httr)
URL = "https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield"

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

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

但它不再有效。

有什么想法可能是错误的或其他建议吗?

1 个答案:

答案 0 :(得分:3)

这不能回答为什么您的代码不再有效的具体问题。这是使用html { margin: 0; } body { background: #DCDCDC; width: 100%; margin: 0; } .carousel-control.left, .carousel-control.right { background-image: none } #firstrow { font-size: 2em; padding: 0px 0 0 0; width: 100%; text-align: center; color: black; position: relative; } #firstrow span { font-family: Impact, Impact5, Charcoal6, sans-serif; font-size: 1.3em; } #firstrow p { font-family: Helvetica, } @media all and (max-width: 2000px) { #headerbutton-nederlands { position: absolute; bottom: 0px; right: -90px; line-height: 40px; } } .planettext { text-align: center; font-size: 1.5em; color: #000; margin-top: 200px; margin-bottom: 30px; width: 100%; background-color: #DCDCDC; font-weight: bold; } .plutotext { text-align: center; font-size: 1.5em; color: #000; margin-top: 100px; margin-bottom: 30px; width: 100%; background-color: #D3D3D3; font-weight: bold; margin: 00; } #pluto { background-color: #D3D3D3; height: 550px; width: 100%; } @media all and (max-width: 900px) { #headerbutton-nederlands { display: none; } #headerbutton-english { display: none; } } @media all and (max-width: 2000px) { #headerbutton-nederlands { position: absolute; bottom: 0px; right: -70px; line-height: 40px; } } @media all and (max-width: 1450px) { #headerbutton-nederlands { position: absolute; font-size: 14px; line-height: 30px; right: -55px; } } @media all and (max-width: 1200px) { #headerbutton-nederlands { font-size: 13px; line-height: 25px; right: -55px; } } @media all and (max-width: 1080px) { #headerbutton-nederlands { font-size: 10px; line-height: 20px; right: -40px; } } @media all and (max-width: 2000px) { #headerbutton-english { position: absolute; bottom: 0px; right: 100px; line-height: 40px; } } @media all and (max-width: 1450px) { #headerbutton-english { position: absolute; font-size: 14px; right: 80px; line-height: 30px } } @media all and (max-width: 1200px) { #headerbutton-english { font-size: 13px; line-height: 25px; right: 50px; } } @media all and (max-width: 1080px) { #headerbutton-english { font-size: 10px; line-height: 20px; right: 50px; } } .planet { margin-bottom: 30px; position: relative; } .planet img { height: 300px; max-width: none; margin-bottom: 20px; } .bannertext { font-size: 1.3em; line-height: 15px; position: relative; } .bannertext h1 { font-size: 2em; } .bannertext2 { font-size: 2em; } .mobileShow { display: none;} .mobileHide { display: inline;} /* Smartphone Portrait and Landscape */ @media only screen and (max-device-width : 480px){ .mobileShow { display: inline;} .mobileHide { display: none;} .planet img{ height: 200px; } } #headerbutton-nederlands { position: absolute; font-weight: bold; -webkit-border-radius: 5px; color: white; background-color: #778899; width: 13%; text-align: center; border: white 2px solid ; } #headerbutton-english { position: absolute; font-weight: bold; -webkit-border-radius: 5px; color: white; background-color: #778899; width: 13%; text-align: center; border: white 2px solid ; } 包的替代方案,它简化了许多抓取操作。特别是在下面,使用类ID CSS选择器rvest来选择表。这使得它更容忍页面格式更改。使用运算符%&gt;%进行链接可以实现非常紧凑的代码。

.t-chart