sitemap生成器:python生成百万级URL的sitemap文件

seofangfa python笔记22,9644阅读模式

手里有一批百万级别的URL没有收录,想做成sitemap提交一下,跟python大神@赵彦刚同学提了这个需求,在我的“变态”要求之下,这个程序近乎完美,无论你给定多少URL,程序会自动判断,按最多50000条URL成生多个sitemap.xml文件,并且能自动生成siteamp索引文件,方便在百度站长平台提交。

在此分享一下这个python小程序。

用法:

sitemap生成器:python生成百万级URL的sitemap文件-图片1

  1. 将需要生成sitemap的URL存为urls.txt,URL一行一个。
  2. 将sitemap.py放到跟url.txt一个文件夹里
  3. 直接运行python sitemap.py,几秒钟之后会在文件当前路径下生成sitemap文件夹,里面就是生成好的sitemap.xml及sitemapindex.xml,sitemapindex.xml在百度站长平台提交就OK了。

在使用这个程序之前,你需要对源码做一点小小的修改,即把原来的host = 'http://seofangfa.com/'改为你自己的域名,并且后面一定要带上斜杠。

效果:

处理319W的URL,只用了6.6秒。

sitemap生成器:python生成百万级URL的sitemap文件-图片2

sitemap生成器:python生成百万级URL的sitemap文件-图片3

python源码:

复制源码,新建一个PY文件就可以使用了。

#coding:utf-8
#python生成sitemap,超过5万条数据自动生成新文件。
# from __future__ import division
#date:2016-3-18
#Author:赵彦刚@zhaoyangang.cn
#PM:方法@seofangfa.com
#
import os,datetime
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

host = 'http://seofangfa.com/' #手动设置你网站的域名,别忘记了结尾的斜杠!
dir = os.popen('mkdir sitemap') #自动新建一个存放sitemap.xml的文件夹,默认叫sitemap,可自行修改。
path = 'sitemap/'#设定sitemap.xml文件存放的路径,别忘记了结尾的斜杠!
lastmod = datetime.date.today()


def add_file(j,f1,host,path):
	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>"%(host,path,file_name,lastmod))
	f=open("%s%s"%(path,file_name),"a")
	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 = c%50000
if file_num==0:
	file_num = c/50000
	print '总共有%s条URL,生成%s个sitemap文件'%(c,file_num)
else:
	file_num = (c/50000)+1
	print '总共有%s条URL,生成%s个sitemap文件'%(c,file_num)


#自动按5W条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),'a')
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>"%(host,path,'sitemap_1.xml',lastmod))
for url in open("urls.txt"):
	url = url.strip()
	i += 1
	if i == 50000 or j == 50000:
		f.write('\n</urlset>')
		f.close()
		i = 0
		f = add_file(j,f1,host,path)
		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()

文章末尾固定信息

weinxin
我的微信
我的微信
微信扫一扫
 
seofangfa
  • 本文由 seofangfa 发表于 2016年3月21日13:46:33
  • 转载请务必保留本文链接:https://wp.fangfa.me/python-note/sitemap.html
评论  2  访客  2
    • themebetter
      themebetter 7

      世上牛人太多了,方法太赞饿了

      • haejin0227
        haejin0227 0

        为什么我不能运行呢?我直接贴了代码 网站都没改
        测试环境:
        Linux(ubuntu 16.04)
        Python 2.7.12

      匿名

      发表评论

      匿名网友
      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
      确定

      拖动滑块以完成验证