国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用Python腳本生成sitemap.xml的實現方法

2020-02-23 04:19:52
字體:
來源:轉載
供稿:網友

安裝lxml

首先需要pip install lxml安裝lxml庫。

如果你在ubuntu上遇到了以下錯誤:

#include "libxml/xmlversion.h"compilation terminated.error: command 'x86_64-linux-gnu-gcc' failed with exit status 1----------------------------------------Cleaning up... Removing temporary dir /tmp/pip_build_root...Command /usr/bin/python -c "import setuptools, 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 --compile failed with error code 1 in /tmp/pip_build_root/lxmlException information:Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main  status = self.run(options, args) File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 283, in run  requirement_set.install(install_options, global_options, root=options.root_path) File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1435, in install  requirement.install(install_options, global_options, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/pip/req.py", line 706, in install  cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False) File "/usr/lib/python2.7/dist-packages/pip/util.py", line 697, in call_subprocess  % (command_desc, proc.returncode, cwd))InstallationError: Command /usr/bin/python -c "import setuptools, 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 --compile failed with error code 1 in /tmp/pip_build_root/lxml

請安裝以下依賴:

sudo apt-get install libxml2-dev libxslt1-dev

Python代碼

下面是生成sitemap和sitemapindex索引的代碼,可以按照需求傳入需要的參數,或者增加字段:

#!/usr/bin/env python# -*- coding:utf-8 -*-import ioimport refrom lxml import etreedef generate_xml(filename, url_list):  """Generate a new xml file use url_list"""  root = etree.Element('urlset',             xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")  for each in url_list:    url = etree.Element('url')    loc = etree.Element('loc')    loc.text = each    url.append(loc)    root.append(url)  header = u'<?xml version="1.0" encoding="UTF-8"?>/n'  s = etree.tostring(root, encoding='utf-8', pretty_print=True)  with io.open(filename, 'w', encoding='utf-8') as f:    f.write(unicode(header+s))def update_xml(filename, url_list):  """Add new url_list to origin xml file."""  f = open(filename, 'r')  lines = [i.strip() for i in f.readlines()]  f.close()  old_url_list = []  for each_line in lines:    d = re.findall('<loc>(http:////.+)<//loc>', each_line)    old_url_list += d  url_list += old_url_list  generate_xml(filename, url_list)def generatr_xml_index(filename, sitemap_list, lastmod_list):  """Generate sitemap index xml file."""  root = etree.Element('sitemapindex',             xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")  for each_sitemap, each_lastmod in zip(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'<?xml version="1.0" encoding="UTF-8"?>/n'  s = etree.tostring(root, encoding='utf-8', pretty_print=True)  with io.open(filename, 'w', encoding='utf-8') as f:    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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台湾省| 铜川市| 望都县| 米林县| 阜宁县| 进贤县| 德兴市| 郴州市| 永靖县| 嵊州市| 福安市| 叙永县| 肃南| 南华县| 股票| 碌曲县| 永昌县| 澄江县| 湘乡市| 忻州市| 宁晋县| 织金县| 卢湾区| 科技| 蒙自县| 准格尔旗| 华安县| 青铜峡市| 林甸县| 泌阳县| 禄劝| 家居| 金沙县| 临澧县| 苍溪县| 贡觉县| 磐石市| 清远市| 白朗县| 清水县| 青河县|