from xml.sax.handler import ContentHandler
from xml.sax import parse
import os
class Dispatcher:
def startElement(self, name, attrs):
self.dispath('start', name, attrs)
def endElement(self, name):
self.dispath('end', name)
def dispath(self, prefix, name, attrs = None): #调用这个方法查找合适的处理程序
mname = prefix + name.capitalize() #构造处理程序的方法名,eg:startPage
dname = 'default' + prefix.capitalize() #构造默认处理程序名字 eg:defaultStart
method = getattr(self, mname, None) #尝试使用getattr获得处理程序,用None作为默认值
if callable(method): args = () #如果可以调用,将一个空元组赋值给args
else:
method = getattr(self, dname, None) #否则舱室用getattr获取默认处理程序,还用None作为默认值,将args设定为只包括标签名的元组
args = name,
if prefix == 'start' : args += attrs, #如果正使用一个起始处理程序,将属性添加到参数元组(args)中
if callable(method): method(*args) #如果处理程序可调用 使用正确参数调用
class WebsiteConstructor(Dispatcher, ContentHandler):
passthrough = False
def __init__(self, directory):
self.directory = [directory]
self.ensureDirectory()
def startPage(self, attrs):
filename = os.path.join(*self.directory + [attrs['name'] + '.html'])
self.out = open(filename, 'w')
self.writeHeader(attrs['title'])
self.passthrough = True
def endPage(self):
self.passthrough = False
self.writeFooter()
self.out.close()
def writeHeader(self, title): #创建开始
self.out.write('<html>\n\t<head>\n\t\t<title>')
self.out.write(title)
self.out.write('</title>\n\t</head>\n\t<body>\n')
def writeFooter(self): #创建结束标签
self.out.write('\n\t</body>\n</html>\n')
def defaultStart(self, name, attrs): #定义开始标签 以及标签属性
if self.passthrough:
self.out.write('<' + name)
for key, val in attrs.items():
self.out.write(' %s="%s"' % (key, val))
self.out.write('>')
def defaultEnd(self, name): #定义结束标签
if self.passthrough:
self.out.write('</%s>' % name)
def ensureDirectory(self): #判断是否存在目录
path = os.path.join(*self.directory)
if not os.path.isdir(path): os.makedirs(path)
def characters(self, chars): #写入文本内容
if self.passthrough: self.out.write(chars)
def startDirectory(self, attrs): #添加目录
self.directory.append(attrs['name'])
self.ensureDirectory()
def endDirectory(self): #释放目录集合
self.directory.pop()
parse('website.xml', WebsiteConstructor('public_html'))
Python 基于XML文件生成静态网页 升级版
最新推荐文章于 2023-09-18 16:02:38 发布