我想使用该rvest软件包从Pro Football Reference网站获取一些数据.首先,让我们从这个网址获取2015年所有游戏的结果http://www.pro-football-reference.com/years/2015/games.htm
library("rvest")
library("dplyr")
#grab table info
url <- "http://www.pro-football-reference.com/years/2015/games.htm"
urlHtml <- url %>% read_html()
dat <- urlHtml %>% html_table(header=TRUE) %>% .[[1]] %>% as_data_frame()
Run Code Online (Sandbox Code Playgroud)
这是你怎么做的?:)
dat可以清理一下.两个变量似乎都有名字的空白.此外,每周之间重复标题行.
colnames(dat) <- c("week", "day", "date", "winner", "at", "loser",
"box", "ptsW", "ptsL", "ydsW", "toW", "ydsL", "toL")
dat2 <- dat %>% filter(!(box == ""))
head(dat2)
Run Code Online (Sandbox Code Playgroud)
看起来不错!
现在让我们看一下个人游戏.在上面的网页上,点击表格第一行中的"Boxscore":9月10日在新英格兰和匹兹堡之间进行的比赛.这需要我们:http://www.pro-football-reference.com/boxscores/201509100nwe.htm.
我想抓住每个玩家的个人快照计数(大约在页面的一半).很确定这些是我们的前两行代码:
gameUrl <- "http://www.pro-football-reference.com/boxscores/201509100nwe.htm"
gameHtml <- gameUrl %>% read_html()
Run Code Online (Sandbox Code Playgroud)
但现在我无法弄清楚如何抓住我想要的特定桌子.我使用选择器小工具突出显示爱国者快照计数表.我通过在几个地方点击表格来执行此操作,然后"取消选择"突出显示的其他表格.我最终得到了一条道路:
#home_snap_counts .right , #home_snap_counts .left, #home_snap_counts .left, #home_snap_counts .tooltip, #home_snap_counts .left
每次尝试都会返回 …
我试图用来rvest提取PGA高尔夫球手的出生日期.我们来试试Stuart Appleby吧.以下是他在ESPN网站上的个人资料http://espn.go.com/golf/player/_/id/11/stuart-appleby.注意他的爆头旁边的他的DOB.
library("rvest")
url <- "http://espn.go.com/golf/player/_/id/11/stuart-appleby"
li_node <- url %>% html %>% html_nodes("li")
Run Code Online (Sandbox Code Playgroud)
他的DOB包含在li_node的第22项中.理想情况下,我不会将[[22]]硬编码到我的程序中,但即使我这样做,也会遇到错误.
li_node[[22]]
Run Code Online (Sandbox Code Playgroud)
显示我想要的信息,但是像:
word(li_node[[22]], ...)
substr(li_node[[22]], ...)
pluck(li_node, 22)
Run Code Online (Sandbox Code Playgroud)
都返回错误:
> word(li_node[[22]], 1)
Error in rep(string, length = n) :
attempt to replicate an object of type 'externalptr'
> substr(li_node[[22]], 1, 2)
Error in as.vector(x, "character") :
cannot coerce type 'externalptr' to vector of type 'character'
> pluck(li_node, 22)
Error in FUN(X[[1L]], ...) :
object of type 'externalptr' is not subsettable
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以让我使用DOB rvest …