生成sitemap.xml

This commit is contained in:
zeek 2020-03-22 11:34:33 +08:00
parent cbf112c295
commit cd07ca54a0

28
sitemap.py Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python3
# coding=utf-8
# -*- coding: utf-8 -*-
import datetime
import re
import get_url
def create_xml(filename, url_list):
header = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
file = open(filename, 'a', encoding='utf-8')
file.writelines(header)
file.close()
for url in url_list:
times = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+00:00")
urls = re.sub(r"&", "&amp;", url)
ment = " <url>\n <loc>%s</loc>\n <lastmod>%s</lastmod>\n <changefreq>weekly</changefreq>\n <priority>0.8</priority>\n </url>\n" % (urls, times)
file = open(filename, 'a', encoding='utf-8')
file.writelines(ment)
last = "</urlset>"
file.writelines(last)
file.close()
if __name__ == '__main__':
get_url.parser(get_url.url_mine_list, 0)
create_xml('sitemap.xml', get_url.url_res_final)