I have two scripts that run as needed while separated. One being a code for PyQt5 GUI application, and second one is a code very similar to this one, with a slight modification to be able to convert contents in case there are any smiley faces that cause problems.
Basically when I press some button in my app window, I expect the second code to be ran.
No matter how hard I tried to fit in the second code, it will always crash my app (or Python). The furthest I was able to get to, is when the second code works after I close my main window - then it runs, and gives me the result I want.
I suspect it has to do with __init__
from second code not being happy that there's already another __init__
from main window running?
As you can tell I'm very confused about the object-oriented part to Python, though no matter how hard I was trying to self-educate for the past few days on the subject, I was unable to fit those two codes together.
My app:
#'all the necessary imports'
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.text = QWebEngineView(self)
self.proc_btn = QPushButton('Proceed')
self.userUrl = QLineEdit(self)
self.labOne = QLabel(self)
self.labTwo = QLabel(self)
self.defUrl = 'default'
self.init_ui()
def init_ui(self):
v_layout = QVBoxLayout()
h_layout = QHBoxLayout()
h_layout.addWidget(self.proc_btn)
h_layout.addWidget(self.userUrl)
v_layout.addWidget(self.text)
v_layout.addWidget(self.labOne)
v_layout.addWidget(self.labTwo)
v_layout.addLayout(h_layout)
self.labOne.setText('URL: ')
self.labTwo.setText('<ENTER LINK PLEASE>')
self.userUrl.returnPressed.connect(self.linkPut)
self.proc_btn.clicked.connect(self.doStuff)
self.setLayout(v_layout)
self.setWindowTitle('Scrapper')
self.show()
def doStuff(self):
print('Doing stuff (expecting 2nd script to be ran)')
def linkPut(self):
newText = (self.userUrl.text())
print('newText: ' + newText)
self.labTwo.setText(newText)
self.defUrl = newText
app = QApplication(sys.argv)
a_window = MainWindow()
sys.exit(app.exec_())
Script I need to implement:
#'all necessary imports'
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_()
print('__init__ WORKS')
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()
_nonbmp = re.compile(r'[\U00010000-\U0010FFFF]')
def _surrogatepair(match):
char = match.group()
assert ord(char) > 0xffff
encoded = char.encode('utf-16-le')
return (
chr(int.from_bytes(encoded[:2], 'little')) +
chr(int.from_bytes(encoded[2:], 'little')))
def with_surrogates(text):
return _nonbmp.sub(_surrogatepair, text)
def main():
page = Page('https://somenicepage.com/')
soup = bs.BeautifulSoup(page.html, 'html.parser'))
longStrCoded = str(soup.find("img", {"class":"pictures"}))
longStr = with_surrogates(longStrCoded)
print('long str: ' + longStr)
extract = longStr.split('src="')[1].split('"')[0]
print(extract)
if __name__ == '__main__': main()