我有一个链接向量,我想从中创建一个 sitemap.xml 文件(文件协议(protocol)可从此处获得:http://www.sitemaps.org/protocol.html)
我了解 sitemap.xml 协议(protocol)(它相当简单),但我不确定为它使用 {XML} 包的最明智的方法是什么。
一个简单的例子:
links <- c("http://r-statistics.com",
"http://www.r-statistics.com/on/r/",
"http://www.r-statistics.com/on/ubuntu/")
如何使用“链接”来构建 sitemap.xml 文件?
最佳答案
这就是您要找的东西吗? (它使用 httr
包获取最后修改的位,并使用非常有用的 whisker
包直接写入 XML。)
require(whisker)
require(httr)
tpl <- '
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{#links}}
<url>
<loc>{{{loc}}}</loc>
<lastmod>{{{lastmod}}}</lastmod>
<changefreq>{{{changefreq}}}</changefreq>
<priority>{{{priority}}}</priority>
</url>
{{/links}}
</urlset>
'
links <- c("http://r-statistics.com", "http://www.r-statistics.com/on/r/", "http://www.r-statistics.com/on/ubuntu/")
map_links <- function(l) {
tmp <- GET(l)
d <- tmp$headers[['last-modified']]
list(loc=l,
lastmod=format(as.Date(d,format="%a, %d %b %Y %H:%M:%S")),
changefreq="monthly",
priority="0.8")
}
links <- lapply(links, map_links)
cat(whisker.render(tpl))
关于r - 如何使用 R 和 {XML} 包创建 sitemap.xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255359/