繁体   English   中英

缓存 read_html

[英]Cache read_html

我试图缓存read_html/xml2以避免在开发过程中淹没服务器

library(digest)
library(xml2)
url = "https://en.wikipedia.org"
cache = digest(url)
if (file.exists(cache)) {
  cat("Reading from cache\n")
  html = readRDS(cache)
} else {
  #Sys.sleep(3)
  cat("Reading from web\n")
  html = xml2::read_html(url) 
  saveRDS(html, file = cache)
}
html

这失败了,因为只有外部指针存储在重新运行时不再有效的文件中。 当我用同样的问题出现memoiseread_html

您始终可以使用as_listas_xml_document来回转换。

library(digest)
library(xml2)
url = "https://en.wikipedia.org"
cache = digest(url)
if (file.exists(cache)) {
  cat("Reading from cache\n")
  html = as_xml_document(readRDS(cache))
} else {
  cat("Reading from web\n")
  html = read_html(url) 
  saveRDS(as_list(html), file = cache)
}
html

或者,查看read_xmlwrite_xml

暂无
暂无

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

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