发送数据时使用 POST 在 R 中下载文件
Download File in R with POST while sending data
我尝试下载一个文件,从服务器获取它同时需要发送数据。在命令行上使用 curl 它工作正常:
curl "https://www.ishares.com/us/product-screener-download.dl" --data "productView=ishares&portfolios=239561-239855"
不幸的是,我无法将它与 R 一起使用。我尝试使用 download.file、download.file 和 libcurl、curl_download 和 httr。 (download.file 无法使用 curl 或 wget,因为我在 window 机器上。)
我用 curl 尝试过但没有用的东西:
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)
我用 httr 尝试过但没有用的东西:
library(httr)
POST("https://www.ishares.com/us/products/etf-product-list", body = list(productView="ishares",portfolios="239561-239855"))
这样不行吗?
URL <- "https://www.ishares.com/us/products/etf-product-list"
values <- list(productView="ishares", portfolios="239561-239855")
POST(URL, body = values)
r <- GET(URL, query = values)
x <- content(r)
在使用 Fiddler 进行了一番探索之后,我发现我需要使用 postfields 发送数据,然后一切正常。
library("curl")
handle <- new_handle()
handle_setopt(handle, customrequest = "POST")
handle_setopt(handle, postfields='productView=ishares&portfolios=239561-239855')
curl_download("https://www.ishares.com/us/product-screener-download.dl", "./data/ishares-us-etf.xls", handle=handle)
所以你应该在httr::POST()
.
中使用一个正确的URL和encode = "form"
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
#> <ON DISK> /tmp/ishares-us-etf.xls
head(readLines(file_path), 5)
#> [1] "<?xml version=\"1.0\"?>"
#> [2] "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"
#> [3] "<Styles>"
#> [4] "<Style ss:ID=\"Default\">"
#> [5] "<Alignment Horizontal=\"Left\"/>"
我尝试下载一个文件,从服务器获取它同时需要发送数据。在命令行上使用 curl 它工作正常:
curl "https://www.ishares.com/us/product-screener-download.dl" --data "productView=ishares&portfolios=239561-239855"
不幸的是,我无法将它与 R 一起使用。我尝试使用 download.file、download.file 和 libcurl、curl_download 和 httr。 (download.file 无法使用 curl 或 wget,因为我在 window 机器上。)
我用 curl 尝试过但没有用的东西:
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)
我用 httr 尝试过但没有用的东西:
library(httr)
POST("https://www.ishares.com/us/products/etf-product-list", body = list(productView="ishares",portfolios="239561-239855"))
这样不行吗?
URL <- "https://www.ishares.com/us/products/etf-product-list"
values <- list(productView="ishares", portfolios="239561-239855")
POST(URL, body = values)
r <- GET(URL, query = values)
x <- content(r)
在使用 Fiddler 进行了一番探索之后,我发现我需要使用 postfields 发送数据,然后一切正常。
library("curl")
handle <- new_handle()
handle_setopt(handle, customrequest = "POST")
handle_setopt(handle, postfields='productView=ishares&portfolios=239561-239855')
curl_download("https://www.ishares.com/us/product-screener-download.dl", "./data/ishares-us-etf.xls", handle=handle)
所以你应该在httr::POST()
.
encode = "form"
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
#> <ON DISK> /tmp/ishares-us-etf.xls
head(readLines(file_path), 5)
#> [1] "<?xml version=\"1.0\"?>"
#> [2] "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"
#> [3] "<Styles>"
#> [4] "<Style ss:ID=\"Default\">"
#> [5] "<Alignment Horizontal=\"Left\"/>"