我试图使用rvest提取PGA高尔夫球手的出生日期。让我们试试斯图尔特·阿普比。这是他在ESPN网站/id/11/stuart-appleby上的个人资料。注意他的道布在他的头部旁边。
library("rvest")
url <- "http://espn.go.com/golf/player/_/id/11/stuart-appleby"
li_node <- url %>% html %>% html_nodes("li")他的道布被包含在li_node第22项中。理想情况下,我不会硬代码[22]到我的程序,但即使我这样做,我会遇到错误。
li_node[[22]]显示我想要的信息,但内容如下:
word(li_node[[22]], ...)
substr(li_node[[22]], ...)
pluck(li_node, 22)所有返回一个错误:
> 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有没有一种简单的方法让我用rvest抓取道布
发布于 2015-02-26 17:58:31
library("rvest")
library("stringr")
url <- "http://espn.go.com/golf/player/_/id/11/stuart-appleby"
url %>%
html %>%
html_nodes(xpath='//li[contains(.,"Age")]') %>%
html_text() %>%
str_extract("[A-Z][a-z]{2,} [0-9]{1,2}, [0-9]{4}")返回:
[1] "May 1, 1971"https://stackoverflow.com/questions/28749234
复制相似问题