利用Python脚本生成sitemap.xml的实现方法
安装lxml
首先需要pipinstalllxml安装lxml库。
如果你在ubuntu上遇到了以下错误:
#include"libxml/xmlversion.h" compilationterminated. error:command'x86_64-linux-gnu-gcc'failedwithexitstatus1 ---------------------------------------- Cleaningup... Removingtemporarydir/tmp/pip_build_root... Command/usr/bin/python-c"importsetuptools,tokenize;__file__='/tmp/pip_build_root/lxml/setup.py';exec(compile(getattr(tokenize,'open',open)(__file__).read().replace('\r\n','\n'),__file__,'exec'))"install--record/tmp/pip-O4cIn6-record/install-record.txt--single-version-externally-managed--compilefailedwitherrorcode1in/tmp/pip_build_root/lxml Exceptioninformation: Traceback(mostrecentcalllast): File"/usr/lib/python2.7/dist-packages/pip/basecommand.py",line122,inmain status=self.run(options,args) File"/usr/lib/python2.7/dist-packages/pip/commands/install.py",line283,inrun requirement_set.install(install_options,global_options,root=options.root_path) File"/usr/lib/python2.7/dist-packages/pip/req.py",line1435,ininstall requirement.install(install_options,global_options,*args,**kwargs) File"/usr/lib/python2.7/dist-packages/pip/req.py",line706,ininstall cwd=self.source_dir,filter_stdout=self._filter_install,show_stdout=False) File"/usr/lib/python2.7/dist-packages/pip/util.py",line697,incall_subprocess %(command_desc,proc.returncode,cwd)) InstallationError:Command/usr/bin/python-c"importsetuptools,tokenize;__file__='/tmp/pip_build_root/lxml/setup.py';exec(compile(getattr(tokenize,'open',open)(__file__).read().replace('\r\n','\n'),__file__,'exec'))"install--record/tmp/pip-O4cIn6-record/install-record.txt--single-version-externally-managed--compilefailedwitherrorcode1in/tmp/pip_build_root/lxml
请安装以下依赖:
sudoapt-getinstalllibxml2-devlibxslt1-dev
Python代码
下面是生成sitemap和sitemapindex索引的代码,可以按照需求传入需要的参数,或者增加字段:
#!/usr/bin/envpython #-*-coding:utf-8-*- importio importre fromlxmlimportetree defgenerate_xml(filename,url_list): """Generateanewxmlfileuseurl_list""" root=etree.Element('urlset', xmlns="http://www.sitemaps.org/schemas/sitemap/0.9") foreachinurl_list: url=etree.Element('url') loc=etree.Element('loc') loc.text=each url.append(loc) root.append(url) header=u'<?xmlversion="1.0"encoding="UTF-8"?>\n' s=etree.tostring(root,encoding='utf-8',pretty_print=True) withio.open(filename,'w',encoding='utf-8')asf: f.write(unicode(header+s)) defupdate_xml(filename,url_list): """Addnewurl_listtooriginxmlfile.""" f=open(filename,'r') lines=[i.strip()foriinf.readlines()] f.close() old_url_list=[] foreach_lineinlines: d=re.findall('<loc>(http:\/\/.+)<\/loc>',each_line) old_url_list+=d url_list+=old_url_list generate_xml(filename,url_list) defgeneratr_xml_index(filename,sitemap_list,lastmod_list): """Generatesitemapindexxmlfile.""" root=etree.Element('sitemapindex', xmlns="http://www.sitemaps.org/schemas/sitemap/0.9") foreach_sitemap,each_lastmodinzip(sitemap_list,lastmod_list): sitemap=etree.Element('sitemap') loc=etree.Element('loc') loc.text=each_sitemap lastmod=etree.Element('lastmod') lastmod.text=each_lastmod sitemap.append(loc) sitemap.append(lastmod) root.append(sitemap) header=u'<?xmlversion="1.0"encoding="UTF-8"?>\n' s=etree.tostring(root,encoding='utf-8',pretty_print=True) withio.open(filename,'w',encoding='utf-8')asf: f.write(unicode(header+s)) if__name__=='__main__': urls=['http://www.baidu.com']*10 mods=['2004-10-01T18:23:17+00:00']*10 generatr_xml_index('index.xml',urls,mods)
效果
生成的效果应该是这种格式:
sitemap格式:
<?xmlversion="1.0"encoding="UTF-8"?> <urlsetxmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/foo.html</loc> </url> </urlset>
sitemapindex格式:
<?xmlversion="1.0"encoding="UTF-8"?> <sitemapindexxmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.example.com/sitemap1.xml.gz</loc> <lastmod>2004-10-01T18:23:17+00:00</lastmod> </sitemap> <sitemap> <loc>http://www.example.com/sitemap2.xml.gz</loc> <lastmod>2005-01-01</lastmod> </sitemap> </sitemapindex>
lastmod时间格式的问题
格式是用ISO8601的标准,如果是linux/unix系统,可以使用以下函数获取
defget_lastmod_time(filename): time_stamp=os.path.getmtime(filename) t=time.localtime(time_stamp) #returntime.strftime('%Y-%m-%dT%H:%M:%S+08:00',t) returntime.strftime('%Y-%m-%dT%H:%M:%SZ',t)
优化
一般来说,用lxml效率低并且内存占用比较大,可以直接用文件的write方法创建。
defgenerate_xml(filename,url_list): withgzip.open(filename,"w")asf: f.write("""<?xmlversion="1.0"encoding="utf-8"?> <urlsetxmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n""") foriinurl_list: f.write("""<url><loc>%s</loc></url>\n"""%i) f.write("""</urlset>""") defappend_xml(filename,url_list): withgzip.open(filename,'r')asf: foreach_lineinf: d=re.findall('<loc>(http:\/\/.+)<\/loc>',each_line) url_list.extend(d) generate_xml(filename,set(url_list)) defmodify_time(filename): time_stamp=os.path.getmtime(filename) t=time.localtime(time_stamp) returntime.strftime('%Y-%m-%dT%H:%M:%S:%SZ',t) defnew_xml(filename,url_list): generate_xml(filename,url_list) root=dirname(filename) withopen(join(dirname(root),"sitemap.xml"),"w")asf: f.write('<?xmlversion="1.0"encoding="utf-8"?>\n<sitemapindexxmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n') foriinglob.glob(join(root,"*.xml.gz")): lastmod=modify_time(i) i=i[len(CONFIG.SITEMAP_PATH):] f.write("<sitemap>\n<loc>http:/%s</loc>\n"%i) f.write("<lastmod>%s</lastmod>\n</sitemap>\n"%lastmod) f.write('</sitemapindex>')
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流。谢谢大家对毛票票的支持。