我有一个带有 js 的页面。在网页上执行 js 后我需要获取 dom。Js 在名为 'sdl' 的 div 中插入文本。我需要在 . js源码中无法解析value,是js生成的。怎么做?对不起我的英语不好。
2 回答
DOM 被暴露(至少在 pyqt >= 4.7.4 中)
document = webview.page().currentFrame().documentElement()
document.findAll("a")
...
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebframe.html
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebelement.html
In Python, you can use Ghost, which is open source and available from github. It's a Python wrapper around the PyQt4+WebKit hack that works pretty well. You can just do
import ghost
g = ghost.Ghost()
g.open('http://stackoverflow.com/')
Now g.content refers to the document, post-rendering.
You can also evaluate JS in the doc with the evaluate method, and it'll return the JS values.
Ghost also exposes the PyQt objects pretty readily, so you can do stuff to a Ghost object that Ghost doesn't implement, and it'll pass through.
I can't remember exactly, but I think something like
g.main_frame.setContent('<b>Hello World</b>')
can be used to set the document, while
g.content = '<b>Hello World</b>'
throws one. It takes some taming, but it doesn't take long to get it working how you want it to.
The Ghost docs suck, but the source is a single file and pretty explanatory. I use Ghost and it's fine. Just don't create more than one Ghost object, else it tends to crash everything.