找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4073|回复: 3

[代码与实例] 网页生成脚本--这里的参数怎么打印不出来?

2

主题

5

帖子

5

积分

贫民

积分
5
mylife18 发表于 2017-11-7 11:37:28 | 显示全部楼层 |阅读模式
本帖最后由 mylife18 于 2017-11-8 11:03 编辑

from xml.sax.handler import ContentHandler
from xml.sax import parse
import os

class Dispatcher:
        def dispatch(self, prefix, name, attrs=None):
                mname = prefix + name.capitalize()
                dname = 'default' + prefix.capitalize()
                method = getattr(self, mname, None)
                if callable(method): args = ()
                else:
                        method = getattr(self, dname, None)
                        args = name,
                if prefix == 'start': args += attrs,
                if callable(method): method(*args)

        def startElement(self, name, attrs):
                self.dispatch('start', name, attrs)

        def endElement(self, name):
                self.dispatch('end', name)

class WebsiteConstructor(Dispatcher, ContentHandler):
        passthrough = False

# initailize the attributes of class "WebsiteConstructor"
        def __init__(self, directory):
                                self.directory = [directory]
                                self.ensureDirectory()

# create directory
        def ensureDirectory(self):
                path = os.path.join(*self.directory)
                print path
                print '----'
                if not os.path.isdir(path): os.makedirs(path)

# write sth into file
        def characters(self, chars):
                if self.passthrough: self.out.write(chars)

        def defaultStart(self, name, attrs):
                #can print name and attrs
                print "Start---name: ", name
                print "Start---attrs: ", attrs
                print "Start---attrs.items(): ", attrs.items()
                if self.passthrough:
                        self.out.write('<' + name)
                        for key, val in attrs.items():
                                self.out.write(' %s="%s"' %(key, val))
                        self.out.write('>')

        def test1(self, name, attrs):
               
#why cann't it print name and attrs ???
                print "test---name: ", name
                print "test---attrs: ", attrs        

        def defaultEnd(self, name):
                #can print name
                print "defaultEnd---name: ", name
                if self.passthrough:
                        self.out.write('</%s>' % name)

        def startDirectory(self, attrs):
                self.directory.append(attrs['name'])
                self.ensureDirectory()

        def endDirectory(self):
                print 'endDirectory'
                self.directory.pop()

        def startPage(self, attrs):
                print 'startPage'
                filename = os.path.join(*self.directory + [attrs['name']+'.html'])
                #filename
                print "filename:", filename
                self.out = open(filename, 'w')
                self.writeHeader(attrs['title'])
                self.passthrough = True

        def endPage(self):
                print 'endPage'
                self.passthrough = False
                self.writeFooter()
                self.out.close()

        def writeHeader(self, title):
                self.out.write('<html>\n <head>\n   <title>')
                self.out.write(title)
                self.out.write('</title>\n </head>\n  <body>\n')



        def writeFooter(self):
                self.out.write('\n </body>\n</html>\n')


parse('website.xml',WebsiteConstructor('public_html'))




版本:Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]

我在尝试读懂这简单的网页生成脚本。py和xml文件都在附件


问题一:
在powershell中,
为何函数「test1」不能打印参数 name 和 attrs 呢?

我的分析:
一,
函数「test1」上下的函数「defaultStart」和「defaultEnd」都打印参数 name 和 attrs,反而处于中间的函数「test1」不能,我很懵逼

二,函数「test1」上下的函数「defaultStart」和「defaultEnd」并不是引用或内建的函数,是用户自定义的,这没什么特别

三,xml文件也是简单的,没什么特别




问题二:
print attrs的结果是:<xml.sax.xmlreader.AttributesImpl instance at 0x00000000029B60C8>
为什么能直接引用作为一个AttributesImpl的实例的attrs,它从哪来呢?

我的分析:
一,
from xml.sax.handler import ContentHandler
from xml.sax import parse
import os
--------------------------------------
导入的模块或函数根本就没有涉及xml.sax.xmlreader.AttributesImpl,它是从哪里来的呢?

二,根据官方文档,我查了导入的ContentHandler和parse都没有联系到AttributesImpl



新手一枚望指导!






website.xml

777 Bytes, 下载次数: 1

ex37-2.py

3.45 KB, 下载次数: 1

回复

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2017-11-9 15:31:32 | 显示全部楼层
parse('website.xml',WebsiteConstructor('public_html'))
调用了defaultStart和defaultEnd,不会去调用你自己创建的函数,至于为什么去查xml手册吧,调用顺序什么的都再里面
回复 支持 反对

使用道具 举报

2

主题

5

帖子

5

积分

贫民

积分
5
mylife18  楼主| 发表于 2017-11-11 15:46:47 | 显示全部楼层
谢谢,原来是我忽略了dispatch这个函数,才搞不清为什么没有调用自定义的函数test1

另外,对于startElement这个函数的原理和例子,官方文档说得好笼统啊!
回复 支持 反对

使用道具 举报

2

主题

5

帖子

5

积分

贫民

积分
5
mylife18  楼主| 发表于 2017-11-11 15:48:50 | 显示全部楼层
剑心无痕 发表于 2017-11-9 15:31
parse('website.xml',WebsiteConstructor('public_html'))
调用了defaultStart和defaultEnd,不会去调用你 ...

谢谢,原来是我忽略了dispatch这个函数,才搞不清为什么没有调用自定义的函数test1

另外,对于startElement这个函数的原理和例子,官方文档说得好笼统啊!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表