大家好,我正在尝试打印到控制台的网址的html。我把密码从教程.py在open source中免费。 这就是课堂:
class LoadHandler(object):
def OnLoadingStateChange(self, browser, is_loading, **_):
"""Called when the loading state has changed."""
if not is_loading:
# Loading is complete. DOM is ready.
# js_print(browser, "Python", "OnLoadingStateChange", "Loading is complete")
print('ready')
print(browser.GetMainFrame().GetText())
我加了最后两行:
^{pr2}$当我运行代码时,我得到一个错误信息:
TypeError: GetText() takes exactly one argument (0 given)
我在文档中看到了需要给函数参数StringVisitor
(https://github.com/cztomczak/cefpython/blob/master/api/Frame.md#gettext)
这是什么问题?
StringVisitor
是实现Visit()
方法的类的对象。下面是您要做的:将
myvisitor
放在OnLoadingStateChange()
函数之外看起来很奇怪,但这是在GetText()
函数返回后保持对象存活的多种方法之一,因为GetText()
是异步的。在您需要在
cefpython
中使用StringVisitor
,因为许多CEF函数是异步的,也就是说,在不完成希望它们执行的操作的情况下立即返回。当实际工作完成时,它们将调用您的回调函数。在您的示例中,当GetText()
准备好的文本准备就绪时,它将调用StringVisitor
对象中的Visit()
方法。这也意味着您需要在程序流中以另一种方式思考。在(我在Need to get HTML source as string CEFPython中回答了一个类似的问题)
相关问题 更多 >
编程相关推荐