如何获取XML文档并使用Python扭曲解析它?

我想要一种快速方法获取URL并在流式传输时解析它.理想情况下,这应该是超快的.我选择的语言是Python.我有一种直觉,扭曲可以做到这一点,但我找不到一个例子.

解决方法:

如果您需要以流方式处理HTTP响应,则有几个选项.

你可以通过downloadPage来做到:

from xml.sax import make_parser
from twisted.web.client import downloadPage

class StreamingXMLParser:
    def __init__(self):
        self._parser = make_parser()

    def write(self, bytes):
        self._parser.Feed(bytes)

    def close(self):
        self._parser.Feed('', True)

parser = StreamingXMLParser()
d = downloadPage(url, parser)
# d fires when the response is completely received

这是有效的,因为downloadPage将响应主体写入传递给它的类文件对象.在这里,使用write和close方法传入对象满足该要求,但是逐步将数据解析为XML而不是将其放在磁盘上.

另一种方法是在HTTPPageGetter级别挂钩. HTTPPageGetter是getPage内部使用的协议.

class StreamingXMLParsingHTTPClient(HTTPPageGetter):
    def connectionMade(self):
        HTTPPageGetter.connectionMade(self)
        self._parser = make_parser()

    def handleResponsePart(self, bytes):
        self._parser.Feed(bytes)

    def handleResponseEnd(self):
        self._parser.Feed('', True)
        self.handleResponse(None) # Whatever you pass to handleResponse will be the result of the Deferred below.

factory = HTTPClientFactory(url)
factory.protocol = StreamingXMLParsingHTTPClient
reactor.connectTCP(host, port, factory)
d = factory.deferred
# d fires when the response is completely received

最后,很快就会有一个新的HTTP客户端API.由于这不是任何版本的一部分,它不像前两种方法那样直接有用,但它更好一些,所以我会把它包含在内,让你了解未来会带来什么. :)新API允许您指定接收响应正文的协议.所以你会做这样的事情:

class StreamingXMLParser(Protocol):
    def __init__(self):
        self.done = Deferred()

    def connectionMade(self):
        self._parser = make_parser()

    def dataReceived(self, bytes):
        self._parser.Feed(bytes)

    def connectionLost(self, reason):
        self._parser.Feed('', True)
        self.done.callback(None)

from twisted.web.client import Agent
from twisted.internet import reactor

agent = Agent(reactor)
d = agent.request('GET', url, headers, None)
def cbRequest(response):
    # You can look at the response headers here if you like.
    protocol = StreamingXMLParser()
    response.deliverBody(protocol)
    return protocol.done
d.addCallback(cbRequest) # d fires when the response is fully received and parsed

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念