Forum Archive

Get HTML from UI.webview

cook

Is it possible to get the HTML from a loaded url in UI.Webview?

JonB

maybe by using eval_js ('document.documentElement.outerHTML'), but i have not tried this.

cook

Thanks @JonB!
It does work- but needs some other stuff.

Either there should be a delay before doing eval_js, or what I found to be better was using re module to check for certain html code. JavaScript onLoad should apparently work but wasn't.

It isn't required to present the UI.view in order to do this, so what we have here is a very simple way in Pythonista to scrape a page that has generated content. I know it's possible with various headless browser modules- but the WebKit is already in UI.webview

Here's what I have that works:


import ui
import console
import re
console.clear()

webb = ui.WebView()
webb.load_url('your URL ')
js = 'document.documentElement.outerHTML'

reg = '' #some text to find to determine if page or script has been loaded/evaluated

while True:
    html = webb.eval_js(js)
    if re.findall(reg, html):
        print html
        break
    else:
        pass