【问题标题】:Rvest Turn Final String Into Multiple RowsRvest 将最终字符串变成多行
【发布时间】:2016-03-31 11:18:53
【问题描述】:

我已使用 rvest 从 ESPNFC.co.uk 上的一场足球比赛中抓取评论,但我正在努力获得所需的最终输出。

library("rvest")
library("xlsx")
espnfc<-html("http://www.espnfc.co.uk/commentary/422421/commentary.html")
  commentary<-espnfc %>%
  html_nodes("#convo-window") %>%
  html_text() 
commentary <- gsub ( "\n", "", commentary)
commentary <- gsub ( "\r", "", commentary)
commentary <- gsub ( "\t", "", commentary)

最终输出是一个巨大的字符串,但是我希望每分钟的动作是数据框中的一行,例如:

"90'Second Half ends, Liverpool 2, Sunderland 2."
"90'Attempt blocked. Adam Johnson (Sunderland) right footed shot from outside the box is blocked. Assisted by Patrick van Aanholt."
"90'Attempt missed. Jordon Ibe (Liverpool) right footed shot from outside the box is close, but misses to the left. Assisted by Mamadou Sakho."
"90'Lucas Leiva (Liverpool) wins a free kick in the attacking half."

我该怎么办?

【问题讨论】:

    标签: html r string gsub rvest


    【解决方案1】:

    使用 css 选择器会让你的生活更轻松

    espnfc<-html("http://www.espnfc.co.uk/commentary/422421/commentary.html")
    commentary<-espnfc %>%
    html_nodes(".comment p") %>%
    html_text() 
    
    minute<-espnfc %>%
    html_nodes(".timestamp p") %>%
    html_text() 
    
    df<-data.frame(minute=minute,commentary=commentary)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2013-10-23
      • 2017-06-18
      相关资源
      最近更新 更多