POST 使用 R 和 httr 的表单

POST a form using R and httr

我尝试使用 R 和包 httrrvest 从本网站 https://www.finanssivalvonta.fi/en/capital-markets/issuers-and-investors/Managers-transactions/shortselling/ 的 csv link 自动导出数据。我尝试了以下代码但没有成功,我不明白我的错误。

当访问网站并使用 chrome 查看已完成的 POST 时,我看到以下 link https://www.finanssivalvonta.fi/api/shortselling/datatable/current/export。但是在 R 中使用相同的 link 时,我的状态代码为 500。我是否必须从 chrome POST 复制所有 header/body?如果是,我该怎么做?

library(httr)
library(rvest)
res <- POST("https://www.finanssivalvonta.fi/api/shortselling/datatable/current/export")
res$status_code
# 500

我也试过使用下面的代码直接导出table但是网页好像没有加载完成

url <- html_session("https://www.finanssivalvonta.fi/en/capital-markets/issuers-and-investors/Managers-transactions/shortselling/")

url %>% html_nodes("table") %>% .[[1]] %>% html_table(fill=T)

# Error in matrix(NA_character_, nrow = n, ncol = maxp) : 
#   invalid 'ncol' value (too large or NA)
# In addition: Warning messages:
#   1: In max(p) : no non-missing arguments to max; returning -Inf
# 2: In matrix(NA_character_, nrow = n, ncol = maxp) :
#   NAs introduced by coercion to integer range

非常感谢

library(rvest)
url<-"https://www.finanssivalvonta.fi/en/capital-markets/issuers-and-investors/Managers-transactions/shortselling/"

# Get the session of the URL
page<-html_session(url)

# RVEST POST the data to the export URL
page<-rvest:::request_POST(page,url="https://www.finanssivalvonta.fi/api/shortselling/datatable/current/export",
                     encode="form",
                     body=list(
                       "draw"= 2,
                       "columns[0][data]"= "positionHolder",
                       "columns[0][searchable]"= "true",
                       "columns[0][orderable]"="false",
                       "columns[0][search][regex]"="false",
                       "columns[1][data]"="issuerName",
                       "columns[1][searchable]"= "true",
                       "columns[1][orderable]"= "false",
                       "columns[1][search][regex]"="false",
                       "columns[2][data]"="isinCode",
                       "columns[2][searchable]"= "true",
                       "columns[2][orderable]"="false",
                       "columns[2][search][regex]"="false",
                       "columns[3][data]"="netShortPositionInPercent",
                       "columns[3][searchable]"="true",
                       "columns[3][orderable]"="false",
                       "columns[3][search][regex]"= "false",
                       "columns[4][data]"="positionDate",
                       "columns[4][searchable]"="true",
                       "columns[4][orderable]"="false",
                       "columns[4][search][regex]"="false",
                       "start"= 0,
                       "length"= 10,
                       "search[regex]"="false",
                       "lang"= "en",
                       "exportOptions[columnData][positionHolder]"= "Position holder",
                       "exportOptions[columnData][issuerName]" ="Name of the issuer",
                       "exportOptions[columnData][isinCode]" = "ISIN",
                       "exportOptions[columnData][netShortPositionInPercent]"="Net short position (%)",
                       "exportOptions[columnData][positionDate]"="Date",
                       "exportOptions[lang]"="en"
                     ))

writeBin(page$response$content , "data_table.csv")

我使用 CHROME 的高级工具来跟踪您单击 "EXPORT in the above URL" 时的网络流量。我使用相同的参数 POST 数据并将结果保存为 CSV。