大家好,我试图打印到控制台的url的html。我从tutorial.py获取了开源中免费的代码。这就是这门课:
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())我增加了最后两行:
print('ready')
print(browser.GetMainFrame().GetText())当我运行代码时,我会得到一个错误提示:
TypeError: GetText()只接受一个参数(0给定)
我在文档中看到了需要提供给函数参数StringVisitor (https://github.com/cztomczak/cefpython/blob/master/api/Frame.md#gettext)的文档。
StringVisitor 是什么?如何解决这个问题?
发布于 2018-02-18 04:27:34
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()函数返回后,它是保持该对象活动的许多方法之一。
您需要在StringVisitor中使用cefpython,因为许多CEF函数都是异步的,也就是说,在没有完成您希望它们做的工作的情况下立即返回。当实际工作完成时,它们将调用您的回调函数。在您的示例中,当GetText()准备的文本准备就绪时,它将调用StringVisitor对象中的Visit()方法。这也意味着你需要在你的程序流程中用另一种方式去思考。
(我在需要将HTML源代码作为字符串CEFPython中回答了一个类似的问题)
https://stackoverflow.com/questions/48317638
复制相似问题