简体   繁体   中英

Save htmlParse object from XML package

I want to save the object that is the result of the htmlParse command. 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.

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'. 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. 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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