您可以下载页面中的所有表并选择所需的表。
library(rvest)
url <- 'https://en.wikipedia.org/wiki/UEFA_Champions_League'
all_tables <- url %>%
read_html() %>%
html_nodes('table.wikitable') %>%
html_table(fill = TRUE)
所以在你的情况下,你需要
all_tables[[4]]
# Player Country Goals Apps Ratio Years ....
#1 1 Cristiano Ronaldo Portugal 128 168 0.76 2003â ....
#2 2 Lionel Messi Argentina 114 140 0.81 2005â ....
#3 3 Raúl Spain 71 142 0.50 1995â2011 ....
#4 4 Karim Benzema France 64 118 0.54 2006â ....
#5 5 Robert Lewandowski Poland 63 85 0.74 2011â ....
#6 6 Ruud van Nistelrooy Netherlands 56 73 0.77 1998â2009 ....
#7 7 Thierry Henry France 50 112 0.45 1997â2010 ....
#8 8 Alfredo Di Stéfano Argentina 49 58 0.84 1955â1964 ....
#9 9 Andriy Shevchenko Ukraine 48 100 0.48 1994â2012 ....
#10 9 Zlatan IbrahimoviÄ Sweden 48 120 0.40 2001â2017 ....
你也可能对
WikipediR
帮助从维基百科检索数据的包。
为了得到
href
从那张表我们可以做的链接
url %>%
read_html() %>%
html_nodes('table.wikitable') %>%
.[[4]] %>%
html_nodes('a') %>%
html_attr('href') %>%
paste0('https://en.wikipedia.org', .)
#[1] "https://en.wikipedia.org/wiki/Cristiano_Ronaldo"
#[2] "https://en.wikipedia.org/wiki/Portugal"
#[3] "https://en.wikipedia.org/wiki/Manchester_United_F.C."
#....