Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Get HTML from UI.webview
-
Is it possible to get the HTML from a loaded url in UI.Webview?
-
maybe by using eval_js ('document.documentElement.outerHTML'), but i have not tried this.
-
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:
<pre><code>
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</code></pre>