【问题标题】:Rvest error: type 'externalptr'Rvest 错误:输入“externalptr”
【发布时间】:2015-04-29 05:31:41
【问题描述】:

我正在尝试使用rvest 来提取 PGA 高尔夫球手的出生日期。让我们试试 Stuart Appleby。这是他在 ESPN 网站http://espn.go.com/golf/player/_/id/11/stuart-appleby 上的个人资料。注意他的爆头旁边的出生日期。

library("rvest")
url <- "http://espn.go.com/golf/player/_/id/11/stuart-appleby"
li_node <- url %>% html %>% html_nodes("li")

他的 DOB 包含在 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 获取 DOB?

【问题讨论】:

    标签: r rvest


    【解决方案1】:
    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"
    

    【讨论】:

    • 非常好。现在提取“1971 年 5 月 1 日”部分,我可以这样做:dob &lt;- url %&gt;% html %&gt;% html_nodes(xpath='//li[contains(.,"Age")]') %&gt;% html_text() substr(dob, 11, nchar(dob) - 10)
    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多