繁体   English   中英

getPage内的延迟对象打印?

[英]getPage inside of deferred object printing?

我想创建一个对特定网址执行getPage的回调,并在操作完成后将其打印出来。 当前,当我打印dpage (请参见下面的代码),我得到了对延迟对象和page内容的引用。

为什么延迟对象的存储位置在打印page和打印d之间改变?

最终,我希望该程序循环浏览我的4个网站,为每个单独的连接创建回调,将其触发,并在准备好页面时打印每个页面。 如果不是要问太多,可以证明这一点吗?

from twisted.web.client import getPage
from twisted.internet import reactor
from twisted.internet.defer import Deferred

def connect(url):
    page = getPage(url)

print page返回<Deferred object at 0x23dcc68>

print d返回<Deferred object at 0x7f1bacacc3b0>

当前结果(以“ http://www.example.com”为例):

d = Deferred()

d.addCallback(connect)

reactor.callWhenRunning(d.callback, 'http://www.example.com')

reactor.callLater(4, reactor.stop)

reactor.run()

您应该使用的是较新的,更复杂的twisted.web.client.Agent而不是较旧的且受限制的getPage 幸运的是,您有一个关于如何使用Agent的非常详尽的教程 ,以及一些诸如ProxyAgentRedirectAgentCookieAgentContentDecoderAgent伴随类。

但是,首先,您可能想熟悉有关如何使用Deferred文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM