我想从UEFA网站(like this one)的几个页面中抓取表格,并尝试将其合并为一个数据帧。数据框应包含单个播放器页面上的所有信息(表格" MATCH LOG")以及带有播放器名称的列(变量)。
Chris的解决方案很好,但我想从页面顶部(选择器' .bigTitle')获取播放器的名称,而不是从框中获取#34; GENERAL INFO&#34 ;。
构成数据帧的代码:
length_links <- length(links)
all_tables <- vector("list",length_links)
for(i in seq_len(length_links)){
page <- html(links[i])
all_tables[[i]] <- as.data.frame(html_table(page))
}
do.call(rbind, all_tables)
感谢您的帮助。
答案 0 :(得分:1)
library(stringr)
c('http://www.uefa.com/teamsandplayers/players/player=1900730/profile/index.html',
'http://www.uefa.com/teamsandplayers/players/player=250078460/profile/index.html',
'http://www.uefa.com/teamsandplayers/players/player=250045034/profile/index.html') -> links
namefind <- function(text){
str_extract(string = text, pattern = "Name.+Position") -> xx
substr(xx, start = 6, stop = nchar(xx)) -> u
u
}
length_links <- length(links)
all_tables <- vector("list",length_links)
for(i in seq_len(length_links)){
html(links[i]) -> q
html_table(q, fill = TRUE)-> tableList
if(length(tableList) == 0) next
for(cc in 1:length(tableList)){
if(all(colnames(tableList[[cc]])[1:3] == c("Date", "Competition", "Phase"))) {
all_tables[[i]] <- tableList[[cc]]
all_tables[[i]]$name <- html_text(html_node(q, css = '.bigTitle'))
}
}
}
do.call(rbind, all_tables) -> k
k[k$Date != "The home team is listed first.",] -> k