原文地址:https://www.douyacun.com/article/ff22421a7c7ea0596d893ebbbf464cab
Sitemap 协议: https://www.sitemaps.org/protocol.html
博客 sitemap: https://www.douyacun.com/sitemap.xml
Github: https://github.com/douyacun/gositemap
pkg.go.dev: https://pkg.go.dev/github.com/douyacun/gositemap
import "github.com/douyacun/gositemap"
go语言实现的sitemap生成工具
st := gositemap.NewSiteMap()
st.SetPretty(true)
url := gositemap.NewUrl()
url.SetLoc("https://www.douyacun.com/")
url.SetLastmod(time.Now())
url.SetChangefreq(Daily)
url.SetPriority(1)
st.AppendUrl(url)
bt, err := st.ToXml()
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%s", bt)
输出
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.douyacun.com/</loc>
<lastmod>2020-04-19T17:28:33+08:00</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
</urlset>
Google 图片扩展功能 [Google Image Support: https://support.google.com/webmasters/answer/178636?hl=zh-Hans&ref_topic=4581190
st := NewSiteMap()
st.SetPretty(true)
url := NewUrl()
url.SetLoc("https://www.douyacun.com/show_image")
// 注意这里url.SetLoc设置网页的网址
// image.SetLoc 设置的是图片访问路径,image的域名和网址域名不一致也可以
_image := NewImage().
SetLoc("https://www.douyacun.com/image1.jpg").
SetTitle("example").
SetCaption("图片的说明。").
SetLicense("https://www.douyacun.com")
url.AppendImage(_image)
_image = NewImage().
SetLoc("https://www.douyacun.com/image2.jpg").
SetTitle("example").
SetCaption("图片的说明。").
SetLicense("https://www.douyacun.com")
url.AppendImage(_image)
st.AppendUrl(url)
data, err := st.ToXml()
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%s", data)
输出
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>https://www.douyacun.com/show_image</loc>
<image:image>
<image:loc>https://www.douyacun.com/image1.jpg</image:loc>
<image:caption>图片的说明。</image:caption>
<image:title>example</image:title>
<image:license>https://www.douyacun.com</image:license>
</image:image>
<image:image>
<image:loc>https://www.douyacun.com/image2.jpg</image:loc>
<image:caption>图片的说明。</image:caption>
<image:title>example</image:title>
<image:license>https://www.douyacun.com</image:license>
</image:image>
</url>
</urlset>
Google 新闻站点地图准则 google news support https://support.google.com/webmasters/answer/178636?hl=zh-Hans&ref_topic=4581190
st := NewSiteMap()
st.SetPretty(true)
url := NewUrl()
url.SetLoc("https://www.douyacun.com/business/article55.html")
url.AppendNews(NewNews().SetName("《示例时报》").
SetTitle("公司 A 和 B 正在进行合并谈判").
SetLanguage("zh-cn").
SetPublicationDate(time.Now()))
st.AppendUrl(url)
data, err := st.ToXml()
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%s", data)
输出
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>https://www.douyacun.com/business/article55.html</loc>
<news:news>
<news:publication>
<news:name>《示例时报》</news:name>
<news:language>zh-cn</news:language>
</news:publication>
<news:publication_date>2020-04-19T17:24:49+08:00</news:publication_date>
<news:title>公司 A 和 B 正在进行合并谈判</news:title>
</news:news>
</url>
</urlset>
视频 Sitemap 及其替代方案 Google Video Support https://support.google.com/webmasters/answer/80471?hl=zh-Hans&ref_topic=4581190
、
或 ``)来识别网页中的视频。请确保相应网页不需要复杂的用户操作或特定的网址片段即可加载,否则 Google 可能找不到它。提示:虽然我们可以通过自然抓取找到网页中内嵌的视频,但您也可以通过发布视频 Sitemap 帮助我们找到您的视频。st := NewSiteMap()
url := NewUrl()
url.Loc = "https://www.douyacun.com"
video := NewVideo().
SetThumbnailLoc("http://www.example.com/thumbs/123.jpg").
SetTitle("适合夏季的烧烤排餐").
SetDescription("小安教您如何每次都能烤出美味牛排").
SetContentLoc("http://streamserver.example.com/video123.mp4").
SetPlayerLoc("http://www.example.com/videoplayer.php?video=123", true).
SetDuration(600*time.Second).
SetExpirationDate(time.Now().Add(time.Hour*24)).
SetRating(4.2).
SetViewCount(12345).
SetPublicationDate(time.Now().Add(-time.Hour*24)).
SetFamilyFriendly(true).
SetRestriction([]string{"IE", "GB", "US", "CN", "CA"}, true).
SetPrice(6.99, "EUR", true, true).
SetRequiresSubscription(true).
SetUploader("GrillyMcGrillerson", "http://www.example.com/users/grillymcgrillerson").
SetLive(true)
url.AppendVideo(video)
st.AppendUrl(url)
bt, err := st.ToXml()
if err != nil {
log.Printf("%v", err)
return
}
fmt.Printf("%s", bt)
输出
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>https://www.douyacun.com</loc>
<video:video>
<video:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
<video:title>适合夏季的烧烤排餐</video:title>
<video:description>小安教您如何每次都能烤出美味牛排</video:description>
<video:content_loc>http://streamserver.example.com/video123.mp4</video:content_loc>
<video:player_loc allow_embed="yes">http://www.example.com/videoplayer.php?video=123</video:player_loc>
<video:duration>600</video:duration>
<video:expiration_date>2020-04-20T17:25:49+08:00</video:expiration_date>
<video:rating>4.2</video:rating>
<video:view_count>12345</video:view_count>
<video:publication_date>2020-04-18T17:25:49+08:00</video:publication_date>
<video:family_friendly>yes</video:family_friendly>
<video:restriction relationship="allow">IE GB US CN CA</video:restriction>
<video:price currency="EUR" type="own" resolution="hd">6.99</video:price>
<video:requires_subscription>yes</video:requires_subscription>
<video:uploader info="http://www.example.com/users/grillymcgrillerson">GrillyMcGrillerson</video:uploader>
<video:live>yes</video:live>
</video:video>
</url>
</urlset>
st := NewSiteMap()
// 默认域名
st.SetDefaultHost("https://www.douyacun.com")
st.SetPretty(true)
// 每个sitemap文件不能多于50000个链接,这里可以自己配置每个文件最多,如果超过MaxLinks,会自动生成sitemap_index.xml文件
// st.SetMaxLinks(2)
st.SetPublicPath("/tmp/gositemap")
url := NewUrl()
url.SetLoc("https://www.douyacun.com/business/article55.html")
url.AppendNews(NewNews().SetName("《示例时报》").
SetTitle("公司 A 和 B 正在进行合并谈判").
SetLanguage("zh-cn").
SetPublicationDate(time.Now()))
st.AppendUrl(url)
path, err := st.Storage()
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Println(path)
生成sitemap.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>https://www.douyacun.com/business/article55.html</loc>
<news:news>
<news:publication>
<news:name>《示例时报》</news:name>
<news:language>zh-cn</news:language>
</news:publication>
<news:publication_date>2020-04-19T17:44:33+08:00</news:publication_date>
<news:title>公司 A 和 B 正在进行合并谈判</news:title>
</news:news>
</url>
</urlset>
拆分较大的站点地图
mapIndex := NewSiteMapIndex()
st1 := NewSiteMap()
st1.SetPretty(true)
st1.SetFilename("sitemap1")
st1.SetCompress(true)
# 以news为例
url := NewUrl()
url.SetLoc("https://www.douyacun.com/business/article55.html")
url.AppendNews(NewNews().SetName("《示例时报》").
SetTitle("公司 A 和 B 正在进行合并谈判").
SetLanguage("zh-cn").
SetPublicationDate(time.Now()))
st1.AppendUrl(url)
url2 := NewUrl().SetLoc("https://www.douyacun.com/business/article56.html")
url2.AppendNews(NewNews().SetName("《示例时报》").
SetTitle("公司 A 和 C 正在进行合并谈判").
SetLanguage("zh-cn").
SetPublicationDate(time.Now()))
st1.AppendUrl(url2)
st1Filename, err := st1.Storage()
if err != nil {
fmt.Printf("%v", err)
return
}
mapIndex.Append("https://www.douyacun.com/" + st1Filename)
filename, err := mapIndex.Storage("/Users/liuning/Documents/github/gositemap/sitemap_index.xml")
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%v", filename)
使用sitemap_index时,建议每个单独的sietmap comporess压缩成.gz文件,SetCompress
后会自动添加 .gz
后缀名 , 生成sitemap1.xml.gz
和 sitemap_index.xml
Api Reference : https://pkg.go.dev/github.com/douyacun/gositemap?tab=doc
MIT@douyacun.