【发布时间】:2014-03-01 18:20:20
【问题描述】:
我已经设法为单个 url 抓取内容,但正在努力为多个 url 自动化它。
这里是如何为单个页面完成的:
library(XML); library(data.table)
theurl <- paste("http://google.com/",url,"/ul",sep="")
convertUTF <- htmlParse(theurl, encoding = "UTF-8")
tables <- readHTMLTable(convertUTF)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
table <- tables[[which.max(n.rows)]]
TableData <- data.table(table)
现在我有一个 url 向量,并想为相应的表抓取每个:
在这里,我读入了包含多个 http 链接的数据:
ur.l <- data.frame(read.csv(file.choose(), header=TRUE, fill=TRUE))
theurl <- matrix(NA, nrow=nrow(ur.l), ncol=1)
for(i in 1:nrow(ur.l)){
url <- as.character(ur.l[i, 2])
}
【问题讨论】:
-
不要在 SO 上使用
file.choose,您应该包含一个简短的网址示例向量。
标签: r loops automation web-scraping lapply