R下载.csv文件绑定到输入框和“单击”按钮(R Download .csv file tied to input boxes and a “click” button)

编程入门 行业动态 更新时间:2024-10-17 05:26:48
R下载.csv文件绑定到输入框和“单击”按钮(R Download .csv file tied to input boxes and a “click” button)

我试图从https://www.fantasysharks.com/apps/bert/forecasts/projections.php下载.csv文件? 直接绑定输入设置(不是静态下载链接)并将其加载到R.填写下拉框后,您必须单击下载.csv按钮。 我发现这使用R来“点击”网页上的下载文件按钮,该按钮详细说明了如何使用POST进行操作,但我无法让它对该代码进行一些修改。 我试过这段代码:

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>

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

but coming up with the error:

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的更简单方法:

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读取文件。

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))

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.

更多推荐

本文发布于:2023-08-04 12:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   单击   输入框   按钮   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!