Web抓取-多个带有R的页面
我需要使用R从web上抓取html表格。每页1000行中有一个表格,总共有316页。 第一个url的链接位于此处:“ 然后我认为只有偏移量在其他URL上增加(100020003000…,316000) 这是我迄今为止为一页工作的代码:Web抓取-多个带有R的页面,r,web-scraping,R,Web Scraping,我需要使用R从web上抓取html表格。每页1000行中有一个表格,总共有316页。 第一个url的链接位于此处:“ 然后我认为只有偏移量在其他URL上增加(100020003000…,316000) 这是我迄今为止为一页工作的代码: library(XML) library(rvest) url <- read_html("http://sumodb.sumogames.de/Query.aspx?show_form=0&columns=6&r
library(XML)
library(rvest)
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")
table <- url %>%
html_nodes(".record") %>%
html_table(fill = TRUE)
table
库(XML)
图书馆(rvest)
url%
html_表(fill=TRUE)
桌子
大表的每个页面上的css选择器为“.record”
最终的目标是将整个表放在一个CSV文件中。以下代码应该可以实现您的目标,但需要警告的是,这将需要很长时间,因为基于web的查询需要对每个页面进行大量加载 代码使用“下一页”、“上一页”和“最后一页”按钮循环浏览页面。需要注意的是,前两页和最后两页具有不同的CSS选择器,因此是手动完成的 完成后需要整理.txt文件
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'))
库(XML)
图书馆(rvest)
#起始页URL
那么问题是什么呢?