Cefpython GetText()函数

2024-09-30 05:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我正在尝试打印到控制台的网址的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)

我在文档中看到了需要给函数参数StringVisitorhttps://github.com/cztomczak/cefpython/blob/master/api/Frame.md#gettext

这是什么问题?


Tags: pybrowser密码ishtml教程opencomplete
1条回答
网友
1楼 · 发布于 2024-09-30 05:21:07

StringVisitor是实现Visit()方法的类的对象。下面是您要做的:

class Visitor(object)
    def Visit(self, value):
        print(value)
myvisitor = Visitor()
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.
            print('ready')
            browser.GetMainFrame().GetText(myvisitor)

myvisitor放在OnLoadingStateChange()函数之外看起来很奇怪,但这是在GetText()函数返回后保持对象存活的多种方法之一,因为GetText()是异步的。在

您需要在cefpython中使用StringVisitor,因为许多CEF函数是异步的,也就是说,在不完成希望它们执行的操作的情况下立即返回。当实际工作完成时,它们将调用您的回调函数。在您的示例中,当GetText()准备好的文本准备就绪时,它将调用StringVisitor对象中的Visit()方法。这也意味着您需要在程序流中以另一种方式思考。在

(我在Need to get HTML source as string CEFPython中回答了一个类似的问题)

相关问题 更多 >

    热门问题