简体   繁体   English

从XML包中保存htmlParse对象

[英]Save htmlParse object from XML package

I want to save the object that is the result of the htmlParse command. 我想保存作为htmlParse命令结果的对象。 Here is some code to illustrate my problem. 这是一些代码来说明我的问题。 Simply, I want to be able to save the parse HTML page to an object and load it into a future session. 简单地说,我希望能够将解析HTML页面保存到对象并将其加载到将来的会话中。

library(XML)
PATH = "/colleges/Bentley-University"
URL <- paste("http://www.cappex.com", PATH, sep="")
doc <- htmlParse(URL)
mylist <- list(doc)
mylist[[1]]
save(mylist, file="mylist.Rdata")
rm(list=ls())
load("mylist.Rdata")

However, when I try to recall the contents of my list, this is the error I get: 但是,当我尝试回忆我的列表中的内容时,这是我得到的错误:

> mylist[[1]]
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file '/var/folders/hv/wtvckymn0230hpsdwylmtf0r0000gn/T//Rtmp8Mrpev/fileed256550e50': No such file or directory

doc cant be saved as it is a pointer to 'C-level nodes'. doc不能保存,因为它是指向“C级节点”的指针。 Putting it in a list doesnt change this fact. 将它放在列表中并不会改变这一事实。 You can write the representation of the XML tree to a string first then save it. 您可以先将XML树的表示形式写入字符串然后保存。 After you can recover the text. 你可以恢复文本。

library(XML)
PATH = "/colleges/Bentley-University"
URL <- paste("http://www.cappex.com", PATH, sep="")
doc <- htmlParse(URL)
saveXML(doc, file="ex.txt")
rm(list=ls())

# recover
doc<-htmlParse('ex.txt')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM