sollte der folgende Code erreichen, was Sie nach, aber es eine sehr lange Zeit gewarnt werden, weil die webbasierte Abfrage für jede einzelne Seite einer intensive Belastung nimmt.
Der Code nutzt die nächsten, vorherigen und letzten Tasten, um durch die Seiten. Ein Vorbehalt dazu sind die ersten zwei und letzten zwei Seiten, die einen anderen CSS-Selektor haben und daher manuell gemacht werden.
Die .txt-Datei muss nach dem Abschluss aufgeräumt werden.
library(XML)
library(rvest)
# Starting page URL
url <- read_html("http://sumodb.sumogames.de/Query.aspx?show_form=0&columns=6&rowcount=5&showheya=on&showshusshin=on&showbirthdate=on&showhatsu=on&showintai=on&showheight=on&showweight=on&showhighest=on")
# URL prefix
urlPrefix <- "http://sumodb.sumogames.de/"
# URL of last page
lastURL <- url %>%
html_nodes('div+ div a+ a') %>%
html_attr("href")
lastURL <- paste0(urlPrefix, lastURL)
lastURL <- read_html(lastURL)
# URL of second to last page
penultimateURL <- lastURL %>%
html_nodes('div+ div a+ a') %>%
html_attr("href")
penultimateURL <- paste0(urlPrefix, penultimateURL)
penultimateURL <- read_html(penultimateURL)
# Table of first page
tabletemp <- url %>%
html_nodes(".record") %>%
html_table(fill = TRUE)
tabletemp <- tabletemp[[1]]
names(tabletemp) <- tabletemp[1, ]
tabletemp <- tabletemp[-1, ]
# Create and write first table to a .txt file
write.table(tabletemp, 'table.txt', row.names = FALSE)
# URL of second page
nextURL <- url %>%
html_nodes('div+ div a:nth-child(1)') %>%
html_attr("href")
nextURL <- paste0(urlPrefix, nextURL)
nextURL <- read_html(nextURL)
# Table of second page
tabletemp <- nextURL %>%
html_nodes(".record") %>%
html_table(fill = TRUE)
tabletemp <- tabletemp[[1]]
names(tabletemp) <- tabletemp[1, ]
tabletemp <- tabletemp[-1, ]
# Append second table to .txt file
write.table(tabletemp, 'table.txt', row.names = FALSE, col.names = FALSE, append = TRUE)
# URL of third page
nextURL <- nextURL %>%
html_nodes('div+ div a:nth-child(2)') %>%
html_attr("href")
nextURL <- paste0(urlPrefix, nextURL)
nextURL <- read_html(nextURL)
# cyle through pages 3 to N - 2
while(html_text(nextURL) != html_text(penultimateURL)){
tabletemp <- nextURL %>%
html_nodes(".record") %>%
html_table(fill = TRUE)
tabletemp <- tabletemp[[1]]
names(tabletemp) <- tabletemp[1, ]
tabletemp <- tabletemp[-1, ]
write.table(tabletemp, 'table.txt', row.names = FALSE, col.names = FALSE, append = TRUE)
nextURL <- nextURL %>%
html_nodes('div+ div a:nth-child(3)') %>%
html_attr("href")
nextURL <- paste0(urlPrefix, nextURL)
nextURL <- read_html(nextURL)
}
# Table of penultimate page
tabletemp <- penultimateURL %>%
html_nodes(".record") %>%
html_table(fill = TRUE)
tabletemp <- tabletemp[[1]]
names(tabletemp) <- tabletemp[1, ]
tabletemp <- tabletemp[-1, ]
# Append penultimate table to .txt file
write.table(tabletemp, 'table.txt', row.names = FALSE, col.names = FALSE, append = TRUE)
# Table of last page
tabletemp <- lastURL %>%
html_nodes(".record") %>%
html_table(fill = TRUE)
tabletemp <- tabletemp[[1]]
names(tabletemp) <- tabletemp[1, ]
tabletemp <- tabletemp[-1, ]
# Append last table to .txt file
write.table(tabletemp, 'table.txt', row.names = FALSE, col.names = FALSE, append = TRUE)
# Checking number of rows in final table
nrow(read.table('table.txt'))
Wenn Sie wollen, dass der Code schneller zu Testzwecken laufen, versuchen Sie auf der fünften zur letzten Seite beginnen oder etwas ähnliches, nur bewusst sein, dass die CSS-Selektoren für die ersten und zweiten Seiten geändert werden müssen.
Ich hoffe das hilft :)
Also, was ist die Frage? – Rentrop