sitemap地图有什么作用?sitemap文件如何产生?
<h1>什么是sitemap
</h1>
<p>
sitemap,即网站地址文件,通常命名为sitemap.xml。
</p>
<h1>
sitemap有什么用?
</h1>
<p>
sitemap对搜索引擎非常友好,如果你的网站上产生了新的文件,使用sitemap可以让搜索引擎快速收录,是网站SEO的最常用手段。
</p>
<div>
<div id="attachment_53607" class="wp-caption aligncenter">
<img class="size-full wp-image-53607" src="https://www.teamczyx.com/article/1603375636.jpg" width="640" height="640" alt="sitemap地图有什么作用?sitemap文件如何产生?" title="sitemap地图有什么作用?sitemap文件如何产生?" />
<p id="caption-attachment-53607" class="wp-caption-text">
sitemap地图有什么作用?sitemap文件如何产生?
</p>
</div>
<p>
<br />
</p>
</div>
<h1>
sitemap文件如何产生?
</h1>
<p>
sitemap文件可以通过工具软件生成,也有网站提供收费服务进行在线生成。
</p>
<p>
今天,我们通过编程,自己实现生成sitemap.xml文件。
</p>
<p>
当然,首先需要了解sitemap文件的格式。
</p>
<p>
国内,最权威的当属百度,我们提交sitemap也都是提交给它。百度给出的sitemap文件格式介绍如下:
</p>
<div>
<div id="attachment_53608" class="wp-caption aligncenter">
<img class="size-full wp-image-53608" src="https://www.teamczyx.com/article/1603375639.jpg" width="640" height="528" alt="sitemap地图有什么作用?sitemap文件如何产生?" title="sitemap地图有什么作用?sitemap文件如何产生?" />
<p id="caption-attachment-53608" class="wp-caption-text">
sitemap地图有什么作用?sitemap文件如何产生?
</p>
</div>
<p>
<br />
</p>
</div>
<p>
依此介绍,我们用NodeJS编码如下:
</p>
<div>
<div id="attachment_53609" class="wp-caption aligncenter">
<img class="size-full wp-image-53609" src="https://www.teamczyx.com/article/1603375641.jpg" width="640" height="369" alt="sitemap地图有什么作用?sitemap文件如何产生?" title="sitemap地图有什么作用?sitemap文件如何产生?" />
<p id="caption-attachment-53609" class="wp-caption-text">
sitemap地图有什么作用?sitemap文件如何产生?
</p>
</div>
<p>
<br />
</p>
</div>
<pre class="prettyprint"><span class="kwd">var</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> </span><span class="str">""</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">var</span><span class="pln"> fs </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">"fs"</span><span class="pun">);</span><span class="pln"> </span><span class="com">//页面目录</span><span class="pln"> fs</span><span class="pun">.</span><span class="pln">readdir</span><span class="pun">(</span><span class="str">"./article/"</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">error</span><span class="pun">,</span><span class="pln">files</span><span class="pun">){</span><span class="pln"> </span><span class="com">//sitemap固定格式,编码设定</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<?xml version="1.0" encoding="utf-8"?>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"
"</span><span class="pun">;</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<urlset>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"
"</span><span class="pun">;</span><span class="pln"> </span><span class="com">//获取每个文章,并写为限定格式</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun"><</span><span class="pln">files</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++){</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<url>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"
"</span><span class="pun">;</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<loc>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"这里改成url地址"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> files</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">'</loc>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"
"</span><span class="pun">;</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'<lastmod>2020-04-12</lastmod>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"
"</span><span class="pun">;</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'</url>'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str">"
"</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> sitemap_data </span><span class="pun">=</span><span class="pln"> sitemap_data </span><span class="pun">+</span><span class="pln"> </span><span class="str">'</urlset>'</span><span class="pun">;</span><span class="pln"> </span><span class="com">//写为sitemap.xml格式</span><span class="pln"> fs</span><span class="pun">.</span><span class="pln">writeFileSync</span><span class="pun">(</span><span class="str">"sitemap.xml"</span><span class="pun">,</span><span class="pln">sitemap_data</span><span class="pun">);</span><span class="pln"> </span><span class="pun">})</span></pre>
<p>
运行,生成sitemap.xml文件如下:
</p>
<div>
<div id="attachment_53610" class="wp-caption aligncenter">
<img class="size-full wp-image-53610" src="https://www.teamczyx.com/article/1603375642.jpg" width="640" height="495" alt="sitemap地图有什么作用?sitemap文件如何产生?" title="sitemap地图有什么作用?sitemap文件如何产生?" />
<p id="caption-attachment-53610" class="wp-caption-text">
sitemap地图有什么作用?sitemap文件如何产生?
</p>
</div>
<p>
<br />
</p>
</div>
<h1>
提交sitemap
</h1>
<p>
提交到自己网站:
</p>
<div>
<div id="attachment_53611" class="wp-caption aligncenter">
<img class="size-full wp-image-53611" src="https://www.teamczyx.com/article/1603375642-1.jpg" width="640" height="627" alt="sitemap地图有什么作用?sitemap文件如何产生?" title="sitemap地图有什么作用?sitemap文件如何产生?" />
<p id="caption-attachment-53611" class="wp-caption-text">
sitemap地图有什么作用?sitemap文件如何产生?
</p>
</div>
<p>
<br />
</p>
</div>
<p>
再到百度站长平台,提交给百度:
</p>
<div>
<div id="attachment_53612" class="wp-caption aligncenter">
<img class="size-full wp-image-53612" src="https://www.teamczyx.com/article/1603375644.jpg" width="640" height="507" alt="sitemap地图有什么作用?sitemap文件如何产生?" title="sitemap地图有什么作用?sitemap文件如何产生?" />
<p id="caption-attachment-53612" class="wp-caption-text">
sitemap地图有什么作用?sitemap文件如何产生?
</p>
</div>
<p>
<br />
</p>
</div>
<p>
这样,百度爬虫,就会很快的去爬取相关的文件,完成收录。然后,自然的,文件就可以被搜索到了。
</p>
页:
[1]