从“导出到excel”中读取数据;URL变成R


Reading data from "Export to excel" URL into R

我正在查看个人球员的共识梦幻足球预测数据:

次优投影数据格式

然而,在右上角有一个"Export"链接,以更清晰的格式指向相同的数据。该链接提示用户将数据保存到.xls文件中。这是我想读进R的数据,我不确定我是否能够直接从URL中读取,或者让R将其保存到excel文件中,然后再读取。我没有偏好,只要我不需要手动保存每个excel文件,因为我计划经常这样做。

我的问题是,我如何在R中创建一个自动过程,我输入导出到excel URL,然后将该数据读取到R中?

首选投影数据格式(提示保存为.xls)

library("gdata")
qb_url <- "http://www.fantasypros.com/nfl/projections/qb.php?export=xls&week=4&min-yes=true&max-yes=true"
download.file(qb_url, "qb.xls")

我正在尝试读取。表函数并跳过前5-6行,但是,由于第一列是全名,有时是足够的,它将名称分成三个单独的列,而我希望它们仅在一个字符列中。我尝试过StringsAsFactors = FALSE和其他代码,但都无济于事。我正在通读这本书。表文档网站,但我无法看到我做错了什么/缺失。

看看rvest。这些页面有一个下载标签:

<a id="export-xls" href="?export=xls&amp;week=4&amp;min-yes=true&amp;max-yes=true" rel="nofollow"><i class="fa fa-file-excel-o"></i> Export</a>

你可以用:

library(rvest)
URL <- "http://www.fantasypros.com/nfl/projections/qb.php?max-yes=true&min-yes=true"
pg <- read_html(URL)
html_attr(html_nodes(pg, "a#export-xls"), "href")

生产:

## [1] "?export=xls&week=4&min-yes=true&max-yes=true"

只需将其附加到domain+query:

http://www.fantasypros.com/nfl/projections/qb.php

,并在R代码中以至少5种方式中的任何一种下载。下面是一个download.file()的例子:

dl_query <- html_attr(html_nodes(pg, "a#export-xls"), "href")
download.file(sprintf("http://www.fantasypros.com/nfl/projections/qb.php%s", dl_query),
          "filenametosaveitas.csv")

我使用".csv",因为它不是一个真正的Excel文件。但是,它是一个丑陋的CSV,所以你必须先用read.table:

对它进行一点按摩。
dat <- read.table("filenametosaveitas.csv", skip=4, header=TRUE, sep="'t")

然后,瞧,你有你的数据:

dplyr::glimpse(dat)
## Observations: 90
## Variables: 33
## $ Player.Name    (fctr) Aaron Rodgers, Cam Newton, Russell Wilson, Andrew Luck, Carson Palmer, P...
## $ Team           (fctr) GB, CAR, SEA, IND, ARI, DEN, ATL, BUF, NO, OAK, CIN, BAL, SD, SF, PHI, N...
## $ pass_att       (dbl) 34.7, 32.1, 29.6, 34.1, 35.6, 38.8, 37.9, 32.2, 38.8, 38.2, 34.6, 37.8, 3...
## $ pass_att.High  (dbl) 39.6, 33.0, 33.8, 39.0, 38.0, 45.5, 41.2, 36.3, 43.6, 39.5, 40.0, 43.0, 3...
## $ pass_att.Low   (dbl) 32.0, 31.4, 27.0, 22.1, 32.2, 35.0, 35.0, 27.2, 35.0, 37.0, 31.0, 35.0, 3...
## $ pass_cmp       (dbl) 22.8, 20.0, 18.3, 21.2, 23.3, 25.4, 24.4, 19.8, 25.4, 23.7, 21.2, 23.5, 2...
## $ pass_cmp.High  (dbl) 25.3, 21.9, 19.3, 24.0, 26.6, 28.2, 25.8, 22.4, 29.2, 26.3, 25.0, 27.0, 2...
## $ pass_cmp.Low   (dbl) 21.0, 19.1, 17.0, 13.2, 21.4, 23.0, 23.0, 18.3, 23.0, 22.0, 18.4, 21.9, 2...
## $ pass_yds       (dbl) 283.2, 240.2, 228.5, 254.6, 279.5, 295.9, 282.8, 232.7, 290.4, 265.8, 245...
## $ pass_yds.High  (dbl) 317.7, 251.0, 235.2, 298.0, 330.0, 325.5, 306.0, 266.3, 324.1, 275.0, 275...
## $ pass_yds.Low   (dbl) 262.0, 231.0, 220.0, 153.9, 258.0, 273.0, 270.0, 208.4, 260.0, 249.7, 227...
## $ pass_tds       (dbl) 2.4, 1.5, 1.5, 1.9, 2.0, 2.0, 1.8, 1.7, 1.8, 1.8, 1.8, 1.7, 1.9, 1.1, 1.7...
## $ pass_tds.High  (dbl) 3.0, 2.0, 1.8, 2.4, 2.1, 2.4, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.2, 2.0...
## $ pass_tds.Low   (dbl) 1.9, 1.3, 1.0, 1.0, 1.9, 1.8, 1.7, 1.5, 1.6, 1.6, 1.5, 1.5, 1.8, 1.0, 1.4...
## $ pass_ints      (dbl) 0.5, 0.8, 0.6, 0.6, 0.9, 0.9, 1.0, 1.0, 1.0, 0.9, 1.0, 0.8, 0.9, 0.7, 0.8...
## $ pass_ints.High (dbl) 0.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.3, 1.1, 1.3, 1.0, 1.1, 1.0, 1.6, 1.2, 1.0...
## $ pass_ints.Low  (dbl) 0.0, 0.7, 0.0, 0.0, 0.7, 0.7, 0.6, 0.8, 0.7, 0.8, 0.9, 0.7, 0.0, 0.0, 0.6...
## $ rush_att       (dbl) 3.8, 7.8, 7.4, 3.9, 1.9, 1.2, 1.9, 5.2, 1.4, 2.3, 3.5, 2.3, 1.3, 6.9, 1.9...
## $ rush_att.High  (dbl) 5.0, 8.5, 9.0, 5.5, 3.1, 1.7, 2.5, 6.0, 2.8, 3.0, 4.2, 3.7, 2.0, 9.0, 2.4...
## $ rush_att.Low   (dbl) 3.0, 7.0, 6.4, 1.5, 1.0, 0.0, 1.0, 3.5, 0.0, 1.7, 3.0, 1.5, 0.0, 6.1, 1.0...
## $ rush_yds       (dbl) 19.4, 41.6, 38.4, 16.9, 5.5, 0.1, 6.8, 26.6, 3.8, 7.5, 11.9, 6.9, 3.1, 40...
## $ rush_yds.High  (dbl) 24.0, 46.8, 40.8, 23.0, 14.3, 2.3, 8.8, 35.1, 10.4, 10.0, 18.3, 16.9, 4.7...
## $ rush_yds.Low   (dbl) 17.3, 39.2, 33.8, 6.2, 2.6, -1.0, 5.0, 14.3, 0.0, 6.0, 8.0, 3.0, 0.0, 35....
## $ rush_tds       (dbl) 0.1, 0.3, 0.4, 0.1, 0.1, 0.0, 0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.0, 0.2, 0.0...
## $ rush_tds.High  (dbl) 0.1, 0.6, 1.0, 0.2, 0.2, 0.0, 0.1, 0.2, 0.1, 0.1, 0.3, 0.2, 0.0, 0.3, 0.1...
## $ rush_tds.Low   (dbl) 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0...
## $ fumbles        (dbl) 0.1, 0.2, 0.1, 0.3, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.2, 0.2, 0.2, 0.3, 0.3...
## $ fumbles.High   (dbl) 0.1, 0.2, 0.1, 0.3, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.2, 0.3, 0.2, 0.3, 0.3...
## $ fumbles.Low    (dbl) 0.1, 0.2, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.2, 0.1, 0.2, 0.2, 0.2...
## $ fpts           (dbl) 22.1, 19.8, 19.8, 18.4, 17.9, 17.9, 17.3, 17.1, 17.1, 16.7, 16.6, 16.4, 1...
## $ fpts.High      (dbl) 28.8, 24.0, 25.8, 24.0, 22.0, 22.5, 22.0, 22.8, 22.0, 22.8, 22.8, 22.3, 2...
## $ fpts.Low       (dbl) 19.5, 16.9, 17.7, 9.8, 16.1, 16.8, 16.8, 14.8, 15.2, 13.7, 13.7, 14.5, 15...
## $ X              (lgl) NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...

泡沫/冲洗/重复你的url