我有一个网页,其中包含一个包含243页的表格。每页有34行。
url
的结构类似于第1页。
http://this-site.com/service/?currpage=1。
我想获取243页的所有数据并将其保存在一个csv
文件中。
到目前为止,我每页使用的代码是
library(XML)
url <- http://this-site.com/service/?currpage=1
service <- as.data.frame(readHTMLTable(url))
head(service)
service <- read_html(url)
如何将数字从1循环到243,以便获取所有页面并将其下载并写入csv?
答案 0 :(得分:1)
library(tidyverse)
library(rvest)
pages <- 1:243
base_url <- "http://this-site.com/service/?currpage="
urls <- paste0(base_url, pages)
get_table <- function(url) {
url %>%
read_html() %>%
html_table() # might not need this???
}
results <- sapply(urls, get_table)
bind_rows(reuslts) %>%
as_data_frame() %>%
write_csv(path = "some/path/somwhere")