[英]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
這失敗了,因為只有外部指針存儲在重新運行時不再有效的文件中。 當我用同樣的問題出現memoise
上read_html
。
您始終可以使用as_list
和as_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_xml
和write_xml
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.