はじめに
XXMLサイトマップ(sitemap.xml)をトップページに配置する事で検索エンジン(google等)にページの情報を通知する事が出来ます。
必ずクロールされるとは限りませんが検索エンジンのインデックスに登録するURLの候補にはなります。
Webクローラ(Anemone)を利用して 簡易 simap ジェネレータを作ってみました。ページが多いサイトでは実行する時にはサーバに負荷が掛かりますので注意が必要です。
ソースコード
pythonで書いた場合
Anemone(Webクローラフレームワーク)から取得したURLの結果を使用してpythonでsitemap.xmlに加工しています。
コマンドを組み合わせる shell scriptに近い作り方かもしれません。
import datetime
import os
URLSET = {
"xsi":"http://www.w3.org/2001/XMLSchema-instance" ,
"schema":'''http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd''',
"xmlns":"http://www.sitemaps.org/schemas/sitemap/0.9"
}
def update_date():
"""
更新日付を取得 ⇒ 現在日付で更新
"""
return datetime.date.today().isoformat()
def create_sitemap_xml(url_list_path,sitemap_path):
"""
サイトのurlのリストから サイトマップ(sitemap.xml)を作成します
"""
sitemap_file = open(sitemap_path,'w')
url_list_file = open(url_list_path,'r')
#header
sitemap_file.write( '''
''' % (URLSET))
sitemap_file.write(os.linesep)
#body
for url in url_list_file:
sitemap_file.write(' %s' % (os.linesep))
sitemap_file.write(
''' %s
%s
daily
0.5
''' % (url.strip(),update_date())
)
sitemap_file.write(' %s' % (os.linesep))
#footer
sitemap_file.write(' ')
if __name__ == "__main__":
#sitemap.xmlを作成
create_sitemap_xml("url_list.txt","sitemap_from_list.xml")
Rubyで書いた場合
依存)
anemone
builder XMLの作成の記述がシンプルに出来る。
Rubyではライブラリを使用している分シンプルで分かりやすいかもしれません。
ソース)
require 'rubygems'
require 'anemone'
require 'builder'
opts = {
:storage => Anemone::Storage.MongoDB,
:skip_query_strings => true,
}
sitemap = ""
xml = Builder::XmlMarkup.new(:target => sitemap, :indent=>2)
xml.instruct!
xml.urlset(:xmlns=>'http://www.sitemaps.org/schemas/sitemap/0.9',
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation" => "http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
) {
Anemone.crawl("http://work.blog.eggplant.org.uk",opts) do |anemone|
anemone.on_every_page do |page|
xml.url {
xml.loc page.url
xml.lastmod Time.now.strftime("%Y-%m-%d")
xml.changefreq "daily"
xml.priority 0.5
}
end
end
}
File.open('sitemap.xml', 'w') do |f|
f.write sitemap
end
参考サイト
※ サイトマッププロトコル ⇒ sitemaps.org※ Generate sitemap.xml using a ruby script