Artyom
Artyom

Reputation: 3063

PyQtWebkit and java script

I have a page with js. I need get dom after js execute at webpage. Js inserts text in the div with name 'sdl'. I need get value between . Value can not be parsed in js source, it is generated by Js. How to do it? Sorry for my english.

Upvotes: 0

Views: 596

Answers (2)

Carl Younger
Carl Younger

Reputation: 3080

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.

Upvotes: 0

tokland
tokland

Reputation: 67900

The DOM is exposed (at least in 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

Upvotes: 1

Related Questions