问题描述
我正在尝试从 https://www 下载 .csv 文件.fantasysharks.com/apps/bert/forecasts/projections.php?直接绑定输入设置(不是静态下载链接)并将其加载到 R 中.填写下拉框后,您必须单击下载 .csv 按钮.我发现这个 使用 R 来点击"网页上的下载文件按钮 详细说明了如何使用 POST 执行此操作,但我无法通过对该代码进行一些修改来使其正常工作.我已经尝试过这个代码:
I am attempting to download a .csv file from https://www.fantasysharks.com/apps/bert/forecasts/projections.php? that is tied directly the input settings (is not a static download link) and load it into R. After the drop boxes are filled in, you then have to click on the download .csv button. I found this Using R to "click" a download file button on a webpage that details a bit how to do it using POST, but am unable to get it to work with some modifications to that code. I have attempted this code:
library(httr)
library(rvest)
library(purrr)
library(dplyr)
POST("https://www.fantasysharks.com/apps/bert/forecasts/projections.php",
body = list('League'=-1, 'Position'=1, 'scoring'=16, 'Segment'=596, 'uid'=4),
encode = "form") -> res
res
但出现错误:
Response [https://www.fantasysharks.com/apps/bert/forecasts/projections.php]
Date: 2017-09-10 15:44
Status: 406
Content-Type: text/html; charset=iso-8859-1
Size: 286 B
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>406 Not Acceptable</title>
</head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /apps/bert/forecasts/projections.php could not be found on t...
</body></html>
推荐答案
以下是从 URL 获取 CSV 的更简单方法:
Here is a simpler way to get a CSV from a URL:
segment <- 596
position <- 1
scoring <- 16
league <- -1
uid <- 4
csv_url <- sprintf("https://www.fantasysharks.com/apps/bert/forecasts/projections.php?csv=1&Segment=%s&Position=%s&scoring=%s&League=%s&uid=%s",segment,position,scoring,league,uid)
res <- read.csv(url(csv_url))
首先,您将参数设置为不同的变量,稍后您将使用这些变量通过 sprintf
生成下载链接.然后使用url
函数从生成的URL下载文件,最后用read.csv
读取文件.
First you set the parameters into different variables that you will later use to generate the download link with sprintf
. Then you use the url
function to download the file from the generated URL and finally read the file with read.csv
.
这篇关于R 下载与输入框和“单击"绑定的 .csv 文件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!