尝试网络抓取一个不变的 URL 数据分布在页面上

Trying to webscrape an unchanging URL with data spread over pages

我是网络抓取的新手。我正在使用的 url 是这个 (https://tsmc.tripura.gov.in/doc_list)。目前,我能够从第一页提取数据。因为 url 是不变的,所以我没有其他页面的标识符来创建数据 table 提取的循环。 这是我的代码:

install.packages("XML")
install.packages("RCurl")
install.packages("rlist")
install.packages("bitops")
library(bitops)
library(XML)
library(RCurl)
url1<- getURL("https://tsmc.tripura.gov.in/doc_list",.opts = 
list(ssl.verifypeer = FALSE))
table1<- readHTMLTable(url1)
table1<- list.clean(table1, fun = is.null, recursive = FALSE)
n.rows <- unlist(lapply(table1, function(t) dim(t)[1]))
table1[[which.max(n.rows)]]
View(table1)
table11= table1[["NULL"]]

请帮忙。谢谢!

也许试试这个解决方案:

url <- "https://tsmc.tripura.gov.in/doc_list?page="
sq <- seq(1, 30) # There appears to be 30 pages so we create a sequence of 1:30 results

links <- paste0(url, sq) #Paste the sequence after the url "page="

store <- NULL
tbl <- NULL

library(rvest) #extract the tables
for(i in links){
  store[[i]] = read_html(i)
  tbl[[i]] = html_table(store[[i]])
}

library(plyr)
df <- ldply(tbl, data.frame) #combine the list of data frames into one large data frame
df$`.id` <- gsub("https://tsmc.tripura.gov.in/doc_list?page=", " ", df$`.id`, fixed = TRUE)

它给出了 8 个变量的 846 个观测值。

编辑:我发现第一个 url 没有序列。为了添加第一页并将其与其余数据一起 rbind 使用以下内容:

firsturl <- "https://tsmc.tripura.gov.in/doc_list"

first_store = read_html(firsturl)
first_tbl = html_table(first_store)
first_df <- as.data.frame(first_tbl)
first_df$`.id` <- 0


df2 <- rbind(first_df, df)