根据网址规则自动生成xml地图,python

作者: loooooong 分类: python 发布时间: 2020-05-13 17:58
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#地图生成
from file_io.long_file import longfile



def baidumap(name,index,max):

    top = '''
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9/" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'''
    content = ''
    for i in range(index, max):
        con = '<url><loc>http://loooooong.com/p-{}.html</loc><mobile:mobile type="pc,mobile"/><changefreq>daily</changefreq><priority>0.8</priority></url>'
        index = i
        content = content + con.format(index)
    end = '''
    </urlset>
    '''
    lf = longfile(file_url='你的目录\loooooong.com\setmap_{}.xml'.format(name),
                  file_str='{}{}{}'.format(top, content, end))
    lf.write()

baidumap(1,1,5000)
baidumap(2,5001,10000)
baidumap(3,10001,15000)
baidumap(4,15001,20000)
baidumap(5,20001,23105)



#!/usr/bin/python
# -*- coding: UTF-8 -*-
#文件操作
class longfile(object):
    '''

    '''

    def __init__(self, file_url=None, file_str=None):
        '''

        :param file_url:
        :param file_str:
        '''
        self.file_url = file_url
        self.file_str = file_str

    def write(self):
        # 打开一个文件
        fo = open(self.file_url, "w")
        fo.write(self.file_str)
        # 关闭打开的文件
        fo.close()

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

专栏推荐