我试图获得HTML源作为字符串从网页网址使用CEFPython,我希望MainFrame
的源内容被抓取和获得字符串
def save_screenshot(browser):
# Browser object provides GetUserData/SetUserData methods
# for storing custom data associated with browser. The
# "OnPaint.buffer_string" data is set in RenderHandler.OnPaint.
buffer_string = browser.GetUserData("OnPaint.buffer_string")
if not buffer_string:
raise Exception("buffer_string is empty, OnPaint never called?")
mainFrame = browser.GetMainFrame()
print("Main frame is ", mainFrame)
# print("buffer string" ,buffer_string)
# visitor object
visitorObj = cef_string()
temp = mainFrame.GetSource(visitorObj).GetString()
print("temp : ", temp)
visitorText = mainFrame.GetText(temp)
siteHTML = mainFrame.GetSource(visitorText)
print("siteHTML is ", siteHTML)
问题:代码没有为siteHTML返回任何内容
发布于 2018-02-17 02:13:26
您的mainframe.GetSource(visitor)
是异步的。因此,您不能从它调用GetString()
。
这就是这样做的方式,不幸的是,你需要以异步的方式思考:
class Visitor(object)
def Visit(self, value):
print("This is the HTML source:")
print(value)
myvisitor = Visitor()
mainFrame = browser.GetMainFrame()
mainFrame.GetSource(myvisitor)
还有一件事需要注意:上面例子中的访问者对象myvisitor
被传递给弱引用中的GetSource()
。换句话说,您必须使该对象保持活动状态,直到传回源。如果您将上述代码片段中的最后三行放入一个函数中,则必须确保该函数在作业完成之前不会返回。
https://stackoverflow.com/questions/44788353
复制相似问题