我尝试下载一个文件,从我需要同时发送数据的服务器获取它。在命令行上使用 curl 可以正常工作:
curl "https://www.ishares.com/us/product-screener-download.dl" --data "productView=ishares&portfolios=239561-239855"
library("curl")
handle <- new_handle()
handle_setopt(handle, customrequest = "POST")
handle_setform(handle, productView="ishares",portfolios="239561-239855")
curl_download("https://www.ishares.com/us/products/etf-product-list", "./data/ishares-us-etf.xls", handle=handle)
library(httr)
POST("https://www.ishares.com/us/products/etf-product-list", body = list(productView="ishares",portfolios="239561-239855"))
câu trả lời hay nhất
所以你应该使用正确的 URL 和 encode = "form"
hiện hữu httr::POST()
.httr
基于@leo 答案的解决方案:
library(httr)
POST("https://www.ishares.com/us/product-screener-download.dl",
body = list(productView = "ishares", portfolios = "239561-239855"),
encode = "form", write_disk("/tmp/ishares-us-etf.xls"))
#> Response [https://www.ishares.com/us/product-screener-download.dl]
#> Date: 2016-02-08 06:52
#> Status: 200
#> Content-Type: application/vnd.ms-excel;charset=UTF-8
#> Size: 13.6 kB
#> /tmp/ishares-us-etf.xls
head(readLines(file_path), 5)
#> [1] ""
#> [2] ""
#> [3] ""
#> [4] "