我尝试下载一个文件,从我需要同时发送数据的服务器上获取它。使用命令行上的curl,它可以正常工作:
curl "https://www.ishares.com/us/product-screener-download.dl" --data "productView=ishares&portfolios=239561-239855"
不幸的是我没有让它与R一起工作。我尝试使用lib.l的download.file,download.file,curl_download和httr。 (带有curl或wget的download.file不能像我在窗口机器上那样工作。)
我尝试过并且没有使用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"))
答案 0 :(得分:3)
因此,您应在encode = "form"
中使用正确的网址httr::POST()
。
httr
解决方案:
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\"/>"
答案 1 :(得分:2)
在用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)
答案 2 :(得分:-1)
这不会做这个工作吗?
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)