凉风有信,秋月无边。
亏我思娇的情绪好比度日如年。

sitemap通过mysql数据库生成网站地图Python代码

#coding:utf-8
#python生成sitemap,超过1万条数据自动生成新文件。
#from __future__ import division
#
import os,datetime
import sys
import pymysql.cursors

reload(sys)
sys.setdefaultencoding('utf-8')

hosts = '域名/'
dir = os.popen('mkdir /data/wwwroot/forwei/www/sitemaps')
path = '/data/wwwroot/forwei/www/sitemaps/'
paths = 'sitemaps/'
lastmod = datetime.date.today()

connection = pymysql.connect(host="127.0.0.1",user="用户名",password="密码",db="表名")
sql = 'SELECT classpath FROM phome_enewsclass union select ztpath from  phome_enewszt union SELECT titleurl  FROM phome_ecms_news'

try:
    with connection.cursor() as cursor:
        cursor.execute(sql)
        cnm = cursor.fetchall()
        pan = open('urls.txt',"w")
        #hu = open('mobile_url.txt',"w")
        for  i  in cnm:
            for item in i:
                if len(item) > 2:
                    if item[0] == "/" :
                        pan.write("域名/%s\n" % item[1:])
                        #hu.write("域名/%s\n" % item[1:])
                    else:
                        if item[0] != "/":
                            pan.write("域名/%s\n" % item)
                            #hu.write("移动端/%s\n" % item)
        pan.close()
        #hu.close()
        cursor.close()

finally:
 connection.close()


def add_file(j,f1,hosts,paths):
 file_name = 'sitemap_%s.xml'%(j)
 f1.write("\n<sitemap>\n<loc>%s%s%s</loc>\n<lastmod>%s</lastmod>\n<priority>0.8</priority>\n</sitemap>"%(hosts,paths,file_name,lastmod))
 f=open("%s%s"%(path,file_name),"w")
 f.write('<?xml version="1.0" encoding="utf-8"?>\n<urlset>')
 return f

#判断总的URL数
c = 0
for i in open('urls.txt'):
 url = i.strip()
 if len(url)==0:
  pass
 else:
  c+=1
print c
#判断需要生成的sitemap个数
file_num = c000
if file_num==0:
 file_num = c/10000
 print '总共有%s条URL,生成%s个sitemap文件'%(c,file_num)
else:
 file_num = (c/10000)+1
 print '总共有%s条URL,生成%s个sitemap文件'%(c,file_num)


#自动按1W条URL生成sitemap,并自动命名为sitemap_1.xml
i = 0
j = 2
f = open('%s/sitemap_1.xml'%(path),'w+')
f.write('<?xml version="1.0" encoding="utf-8"?>\n<urlset>')
f1 = open('%s/sitemapindex.xml'%(path),'w')
f1.write('<?xml version="1.0" encoding="utf-8"?>\n<sitemapindex>')
f1.write("\n<sitemap>\n<loc>%s%s%s</loc>\n<lastmod>%s</lastmod>\n<priority>0.8</priority>\n</sitemap>"%(hosts,paths,'sitemap_1.xml',lastmod))
for url in open("urls.txt"):
 url = url.strip()
 i += 1
 if i == 10000 or j == 10000:
  f.write('\n</urlset>')
  f.close()
  i = 0
  f = add_file(j,f1,hosts,paths)
  j += 1
 f.write("\n<url>\n<loc>%s</loc>\n<lastmod>%s</lastmod>\n<priority>0.8</priority>\n</url>"%(url,lastmod))

f.write('\n</urlset>')
f1.write('\n</sitemapindex>')
f1.close()

 

赞(0) 打赏
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的内容,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。本博客资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。如果本文导致的版权问题以及内容纠错问题请联系站长QQ:1004619 | 点此给我发送邮件
本文标题:《sitemap通过mysql数据库生成网站地图Python代码》
本文地址:https://www.1004619.com/mianfei/zhide/sitemap-tong-guo-mysql-shu-ju-ku-sheng-cheng-wang-zhan-di-tu-python-dai-ma/

相关推荐

  • 暂无文章