P
P
Pavel Chirkov2020-01-03 10:24:29
Python
Pavel Chirkov, 2020-01-03 10:24:29

PyQt5 using QWebEngineView how to get page html?

Good afternoon everyone!
I use PyQt5 in it as a QWebEngineView browser.
I load the page - and try to get the html - but it doesn't work, that's what I'm doing wrong.
Here is my code

import sys
import time
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication


def callback_function(html):
    print(html)


def on_load_finished():
    web.page().runJavaScript("window.onload = function(){alert(document.documentElement.innerHTML);};", callback_function)


app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://yandex.ru"))
web.show()
web.resize(640, 480)
web.loadFinished.connect(on_load_finished)

sys.exit(app.exec_())

Am I doing everything right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Chirkov, 2020-01-03
@pawelch

Found what suits me

import webview
from threading import Thread


def thread_fun():
  while webview.evaluate_js('document.readyState') != "complete":
    # wait for page to load
    time.sleep(0.5)

  # ask for a bounding rect
  bounding_rect = webview.evaluate_js('''
    document.querySelector("img.central-featured-logo").getBoundingClientRect()
  ''');  
  webview.destroy_window()

  print(bounding_rect)


thread = Thread(target=thread_fun)
thread.start()

webview.create_window(title="a title", url="http://wikipedia.org", width=500, height=700)

thread.join()

but now the error is
module 'webview' has no attribute 'evaluate_js'
Can anyone tell me what's wrong?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question