【发布时间】:2024-09-23 02:25:02
【问题描述】:
我在 R 中创建了一系列使用特定 URL 完成工作的命令。我想在一个单独的文本文件中的 URL 列表上迭代一系列命令。如何一次将列表调用到命令中?
我不知道这个编程操作的正确术语是什么。我研究过脚本和批处理编程,但这不是我想做的。
# URL that comes from list
URL <- "http://www.urlfromlist.com"
# Load URL
theurl <- getURL(URL,.opts = list(ssl.verifypeer = FALSE) )
# Read the tables
tables <- readHTMLTable(theurl)
# Create a list
tables <- list.clean(tables, fun = is.null, recursive = FALSE)
# Convert the list to a data frame
df <- do.call(rbind.data.frame, tables)
# Save dataframe out as a csv file
write.csv(df2, file = dynamicname, row.names=FALSE)
上面的代码就是我正在做的。第一个变量每次都需要是来自列表的不同 URL - 冲洗并重复。谢谢!
更新的代码 - 这仍然没有写出任何文件,而是运行。
# Function to pull tables from list of URLs
URLfunction<- function(x){
# URL that comes from list
URL <- x
# Load URL
theurl <- RCurl::getURL(URL,.opts = list(ssl.verifypeer = FALSE) )
# Read the tables
tables <- XML::readHTMLTable(theurl)
# Create a list
tables <- rlist::list.clean(tables, fun = is.null, recursive = FALSE)
# Convert the list to a data frame
df <- do.call(rbind,tables)
# Split date and time column out
df2 <- separate(df, "Date / Time", c("Date", "Time"), sep = " ")
# Fill the missing column with text, in this case shapename
shapename <- qdapRegex::ex_between(URL, "ndxs", ".html")
df2$Shape <- shapename
# Save dataframe out as a csv file
write.csv(result, paste0(shapename, '.csv', row.names=FALSE))
return(df2)
}
URL <- read.csv("PATH", header = FALSE)
purrr::map_df(URL, URLfunction) ## Also tried purrr::map_df(URL[,1], URLfunction)
【问题讨论】:
-
URL 列表是在本地计算机上的文本文档中还是在 URL 中?
-
嗨,安德鲁,是的,网址在 csv 中,