【问题标题】:POST request issue with httr: desired table not retrievedhttr 的 POST 请求问题:未检索到所需的表
【发布时间】:2021-09-10 13:35:38
【问题描述】:

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

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

预期输出:带有历史数据的 html 表格:sample table output

脚本逻辑

  • 使用httr 发送POST 查询
  • 使用 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]])

您可能会注意到,与原始网页 https://www.investing.com/rates-bonds/austria-1-year-bond-yield-historical-data 相比,R 脚本中使用的 URL 使用了不同的 URL https://www.investing.com/instruments/HistoricalDataAjax。其原因显然是设置开始日期和结束日期时 POST 请求中使用的链接。您可能会在下面的屏幕截图中看到这一点:

XHR request header when setting the start and end dates

据我所知,当用户指定特定证券的日期时,网站会向HistoricalDataAjax 发送查询,并在请求正文中指定证券/资产的参数和标识符:Example of the requests's body after selecting dates

【问题讨论】:

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

标签: jquery r post httr web-mining


【解决方案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%  

【讨论】:

  • 嗨@Nad Pat,感谢您的光临!此方法仅读取最近 30 天的数据。我需要一个脚本,它允许使用参数检索数据:从 t 到 T。这就是原因,我使用了这个 Ajax 链接,它在技术上用我可以传入的子表参数替换查询。
  • RSelenium怎么样?
  • 我记得您在原始问题帖子的评论中建议了RSelenium。我在那里解释了为什么这对我来说不是一个理想的解决方案。简而言之,如果有一种更简单的方法可以在没有依赖关系的情况下提取数据——就像在investpy 中一样,我想在 r 中复制它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多