描述:尝试使用httr
库从 Investing.com 检索历史数据
原始页面:https ://www.investing.com/rates-bonds/austria-1-year-bond-yield-historical-data
预期输出:带有历史数据的 html 表:示例表输出
脚本逻辑:
- 发送
POST
查询httr
read_html
用html_table
方法美化方法的输出
问题:
- 脚本从主页而不是实际的历史表中检索表
代码:
library(httr)
url <- 'https://www.investing.com/instruments/HistoricalDataAjax'
# mimic XHR POST request implemented in the investing.com website
http_resp <- POST(url = url,
body = list(
curr_id = "23859",
smlID = "202274",
header = "Austria+1-Year+Bond+Yield+Historical+Data",
st_date = "08/01/2021", # MM/DD/YYYY format
end_date = "08/20/2021",
interval_sec = "Daily",
sort_col = "date",
sort_ord = "DESC",
action = "historical_data"
)
)
# parse the returned XML
html_doc <- read_html(http_resp)
print(html_table(html_doc)[[1]])
您可能会注意到 R 脚本中使用的 URLhttps://www.investing.com/instruments/HistoricalDataAjax
与原始 web-page 相比使用了不同的 URL https://www.investing.com/rates-bonds/austria-1-year-bond-yield-historical-data
。其原因显然是设置开始日期和结束日期时 POST 请求中使用的链接。您可能会在下面的屏幕截图中看到这一点:
据我所知,当用户指定特定证券的日期时,网站会向HistoricalDataAjax
请求正文中指定的证券/资产的参数和标识符发送查询:选择日期后请求正文的示例