【问题标题】:Parsing multiple files for HTML tables and appending to single file in R解析 HTML 表的多个文件并附加到 R 中的单个文件
【发布时间】:2011-05-23 19:55:52
【问题描述】:

这是我正在尝试执行的 R 中的一个小项目。我已经抓取了几百个 html 页面。我可以使用带有 R 的 XML 库中的 reaHTMLTable 函数来读取我感兴趣的表。但是我在编写 for 循环以遍历目录、从每个文件中获取表并附加它们时遇到了麻烦到单个 CSV 文件。

我已经成功地遍历文件并将每个表保存到单个 txt 文件中(我觉得这至少是一个开始):

library(XML)     # htmlTreeParse
  parentpath <- "Z:/scraping"
  setwd(parentpath)
  filenames <- list.files()
      for (targetfile in filenames){
      setwd(parentpath)
      data = readHTMLTable(targetfile)
      outputfile <- paste(targetfile,'.txt', sep="")
write.table (data[6], file = outputfile , sep = "\t", quote=TRUE)

【问题讨论】:

  • 你试过append = TRUE 使用单个文件名吗?

标签: html parsing r loops


【解决方案1】:

write.table 中的 append=TRUE 选项不应该为您解决问题吗?您可以通过查找 ?write.table 来了解它。

【讨论】:

  • 是的......你是对的。 append=TRUE 可以解决问题。我相信我之前也尝试过,但是我忘记了代码末尾的尾括号 } 并且没有返回任何内容。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2014-11-18
  • 2014-05-05
  • 2021-11-22
  • 2016-04-29
相关资源
最近更新 更多