httr 的 POST 请求问题:未检索到所需的表

POST request issue with httr: desired table not retrieved

提问人:Artem Kochnev 提问时间:9/10/2021 最后编辑:Artem Kochnev 更新时间:12/9/2021 访问量:114

问:

描述:尝试使用库从 Investing.com 检索历史数据httr

原文页面https://www.investing.com/rates-bonds/austria-1-year-bond-yield-historical-data

预期输出:包含历史数据的 html 表:示例表输出

脚本逻辑

  • 发送查询POSThttr
  • 使用 html_table 方法美化read_html方法的输出

问题

  • 脚本从主页检索表,而不是从实际的历史记录表中检索表

代码

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 脚本中使用的 URL 使用的 URL 不同。造成这种情况的原因显然是设置开始和结束日期时 POST 请求中使用的链接。您可能会在下面的屏幕截图中看到这一点:https://www.investing.com/instruments/HistoricalDataAjaxhttps://www.investing.com/rates-bonds/austria-1-year-bond-yield-historical-data

设置开始日期和结束日期时的 XHR 请求标头

据我所知,当用户为特定证券指定日期时,网站会发送一个查询,其中包含请求正文中指定的证券/资产的参数和标识符: 选择日期后的请求正文示例HistoricalDataAjax

jquery r post httr 网络挖掘

评论

0赞 Nad Pat 9/19/2021
该链接正在返回主页。你能提供一个适当的链接吗?https://www.investing.com/instruments/HistoricalDataAjax
0赞 Artem Kochnev 9/20/2021
嗨,@NadPat!很高兴看到这篇文章引起了您的注意。这是原始链接:investing.com/rates-bonds/...不过,请考虑一下,我使用了 HistoricaDataAjax,因为这就是原始 Header 请求似乎 POST 查询的方式。我将在原始帖子中添加更多说明以使其清晰。
0赞 Nad Pat 9/20/2021
要下载数据,您需要登录。解决方案适合您吗?RSelenium
0赞 Artem Kochnev 9/20/2021
我不想使用Selenium。首先,因为它引入了对用户计算机的额外依赖。其次,我预计执行速度会更慢。第三,我知道可以使用 POST 请求绕过登录过程:当通过 XHR 在 Web 上更新数据时,会得到 HTML 表。真正的问题是正确地获取它。我知道一个事实,它完全是这样实现的。 虽然是一个 Python 库,而我需要在 R 中实现。investpyinvestpy

答:

0赞 Nad Pat 12/9/2021 #1

你可以把桌子放进去,

https://www.investing.com/rates-bonds/austria-1-year-bond-yield-historical-data

rvest

library(rvest)
df = url %>%
  read_html() %>% 
  html_table()

df[[1]]
# A tibble: 25 x 6
   Date          Price   Open   High    Low `Change %`
   <chr>         <dbl>  <dbl>  <dbl>  <dbl> <chr>     
 1 Dec 09, 2021 -0.669 -0.672 -0.633 -0.695 11.69%    
 2 Dec 08, 2021 -0.599 -0.6   -0.549 -0.647 -2.28%    
 3 Dec 07, 2021 -0.613 -0.621 -0.536 -0.656 -7.54%    
 4 Dec 06, 2021 -0.663 -0.648 -0.565 -0.687 -0.30%    
 5 Dec 03, 2021 -0.665 -0.681 -0.577 -0.684 0.45%     
 6 Dec 02, 2021 -0.662 -0.59  -0.573 -0.669 0.46%     
 7 Dec 01, 2021 -0.659 -0.608 -0.577 -0.685 1.70%     
 8 Nov 30, 2021 -0.648 -0.697 -0.601 -0.736 -4.85%    
 9 Nov 29, 2021 -0.681 -0.715 -0.647 -0.745 -12.47%   
10 Nov 27, 2021 -0.778 -0.701 -0.701 -0.778 7.61%  

评论

0赞 Artem Kochnev 12/17/2021
嗨,帕特@Nad,感谢您的光临!此方法仅读取过去 30 天的数据。我需要一个脚本,它允许使用参数检索数据:从 t 到 T。这就是为什么,我使用了这个 Ajax 链接,它在技术上用我可以传入的可子集参数替换了查询。
0赞 Nad Pat 12/17/2021
使用怎么样?RSelenium
0赞 Artem Kochnev 1/10/2022
我记得你在原始问题帖子的评论中提出了建议。我在那里解释了为什么这对我来说不是一个理想的解决方案。简而言之,如果有一种更简单的方法可以在没有依赖项的情况下提取数据 - 这是可能的,我想在 r 中复制它。RSeleniuminvestpy