Имам списък от 38 000+ URL адреса. Всеки url има таблица, която бих искал да изтрия. Например:
library(rvest)
library(magrittr)
library(plyr)
#Doing URLs one by one
url<-"http://www.acpafl.org/ParcelResults.asp?Parcel=00024-006-000"
##GET SALES DATA
pricesdata <- read_html(url) %>% html_nodes(xpath = "//table[4]") %>% html_table(fill=TRUE)
library(plyr)
df <- ldply(pricesdata, data.frame)
Бих искал да обобщя това за всички URL адреси в Parcels_ID.txt.
#(1) Step one is to generate a list of urls that we want to scrape data from
parcels <- read.csv(file="Parcels_ID.txt", sep="\t", header=TRUE, stringsAsFactors=FALSE) #import the data
parcelIDs <- as.vector(parcels$PARCELID) #reformat the column as a vector containing parcel IDs as individual elements of the vector
parcels$url = paste("http://www.acpafl.org/ParcelResults.asp?Parcel=", parcelIDs, sep="") #paste the web address and the parcel ID together to get the link to the parcel on the website
Сега, след като го имам, бих искал да напиша цикъл, който минава през всеки url и изтегля таблицата, след което поставя резултатите в списък с кадри с данни. Тук имам проблем:
#(2) Step to is to write a for loop that will scrape the tables from the individual pages
compiled<-list()
for (i in seq_along(parcels$url)){
##GET SALES DATA
pricesdata <- read_html(parcels$url[i]) %>% html_nodes(xpath = "//table[4]") %>% html_table(fill=TRUE)
compiled[[i]] <- ldply(pricesdata, data.frame)
}
Този код никога не завършва. Ще оценя всички орлови очи, които могат да забележат грешки или проблеми, или всякакви предложения относно най-добрите практики за писане на този for цикъл, който прави рамка с данни от таблиците, извлечени от уебсайтовете.
Благодаря ти