V
V
Valeks842017-09-11 16:03:07
Python
Valeks84, 2017-09-11 16:03:07

Crash when rendering JS page with PyQt5?

Hey!
I was looking for opportunities to run JS from html and found a wonderful option, much simpler than any selenium, etc. The PyQt5 library is used
Here is the actual code

import bs4 as bs
import sys
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl

class Page(QWebEnginePage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ''
        self.loadFinished.connect(self._on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def _on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print('Load finished')

    def Callable(self, html_str):
        self.html = html_str
        self.app.quit()


def main():
    page = Page('https://pythonprogramming.net/parsememcparseface/')
    soup = bs.BeautifulSoup(page.html, 'html.parser')
    js_test = soup.find('p', class_='jstest')
    print js_test.text

if __name__ == '__main__': main()

Everything works fine, BUT only when creating the FIRST object of the Client class. When creating the next object (well, you need to parse the next page), an error occurs Process finished with exit code 139 (interrupted by signal 11: SIGSEGV). Although the program ends and gives the result, but still this error pops up a window that the program crash.
Mac OS Sierra

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valeks84, 2017-09-11
@Valeks84

The reason for the failure, the call to create this object
twice self.app = QApplication(sys.argv)
If it is taken out of the class definition, then everything works. But how to work with the class now if it is in an external library (module)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question