M
M
Muilton2020-05-09 14:39:35
Python
Muilton, 2020-05-09 14:39:35

PyQT5. WebEngineView. I start a cycle-> 5 empty forms open and then pages open. How to make pages load sequentially?

there is a MainWindow, the address and number of threads are filled in, in a cycle these threads open in new, for example 5, windows.
All 5 empty windows open immediately, and only then they simultaneously start knocking on the network (i.e., the webengine engine is working) and opens approx.
But it is necessary that they open sequentially, i.e. a dummy opens -> the page is loaded in it, a new dummy opens -> the page is loaded.

I thought the problem was due to synchronism, but it turns out that the webengine itself is implemented asynchronously.
Tried all sorts of timers, threads - nothing helped.
How to force webengine to start?
MB queue, priority?

from PyQt5.QtWidgets import QApplication, QWidget, QGroupBox, QMainWindow, QPushButton, \
    QLabel, QPlainTextEdit, QCheckBox, QMessageBox, QSpinBox, QProgressBar
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage, QWebEngineSettings
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QUrl, QTimer, QThread
from PyQt5 import QtNetwork
import random
import sys
import time

class Ui_SecondWindow(object):
    def setupUi(self, MainWindow, link, useragent=None, proxy_address=None):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(500, 500)
        MainWindow.move(10, 10)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.resize(500, 500)
        self.centralwidget.move(0, 0)
        self.centralwidget.setObjectName("centralwidget")

        if proxy_address:
            host, port = proxy_address.split(':')

        self.viewer = QWebEngineView(self.centralwidget)
        self.profile = QWebEngineProfile(self.centralwidget)
        self.webpage = QWebEnginePage(self.profile, self.viewer)

        self.viewer.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        self.viewer.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, True)

        if useragent:
            self.profile.setHttpUserAgent(useragent)

        if proxy_address:
            proxy = QtNetwork.QNetworkProxy()
            proxy.setType(QtNetwork.QNetworkProxy.HttpProxy)
            proxy.setHostName(host)
            proxy.setPort(int(port))
            QtNetwork.QNetworkProxy.setApplicationProxy(proxy)

        self.viewer.setPage(self.webpage)
        self.viewer.load(QUrl(link))
        self.viewer.resize(500, 500)


class MainWindow(QMainWindow):
    def __init__(self):

        super().__init__()
        self.resize(700, 600)
        self.setWindowTitle('View booster')

        self.label_links = QLabel(self)
        self.label_links.move(390, 10)
        self.label_links.resize(181, 16)
        self.label_links.setFont(QFont('Arial', 10))
        self.label_links.setText('Links (video or website)')

        self.text_links = QPlainTextEdit(self)
        self.text_links.move(380, 30)
        self.text_links.resize(301, 151)
        self.text_links.setFont(QFont('Arial', 10))
        self.text_links.setPlainText('2ip.ru')

        self.group_box = QGroupBox(self)
        self.group_box.move(380, 210)
        self.group_box.resize(301, 80)
        self.group_box.setFont(QFont('Arial', 10))
        self.group_box.setTitle('Proxy')

        self.checkbox_proxy = QCheckBox(self.group_box)
        self.checkbox_proxy.move(20, 40)
        self.checkbox_proxy.resize(131, 17)
        self.checkbox_proxy.setFont(QFont('Arial', 10))
        self.checkbox_proxy.setText('Use proxy')

        self.checkbox_os_browser = QCheckBox(self.group_box)
        self.checkbox_os_browser.move(120, 40)
        self.checkbox_os_browser.resize(201, 17)
        self.checkbox_os_browser.setFont(QFont('Arial', 10))
        self.checkbox_os_browser.setText('Use fake OS and Browser')

        self.label_proxylist = QLabel(self)
        self.label_proxylist.move(380, 300)
        self.label_proxylist.resize(181, 16)
        self.label_proxylist.setFont(QFont('Arial', 10))
        self.label_proxylist.setText('List of proxy')

        self.text_proxy = QPlainTextEdit(self)
        self.text_proxy.move(380, 320)
        self.text_proxy.resize(301, 151)
        self.text_proxy.setFont(QFont('Arial', 10))

        self.label_delay = QLabel(self)
        self.label_delay.move(430, 480)
        self.label_delay.resize(71, 21)
        self.label_delay.setFont(QFont('Arial', 10))
        self.label_delay.setText('Delay')

        self.spinbox_delay = QSpinBox(self)
        self.spinbox_delay.move(410, 500)
        self.spinbox_delay.resize(91, 31)
        self.spinbox_delay.setFont(QFont('Arial', 10))

        self.label_threads = QLabel(self)
        self.label_threads.move(580, 480)
        self.label_threads.resize(81, 21)
        self.label_threads.setFont(QFont('Arial', 10))
        self.label_threads.setText('Threads')

        self.spinbox_threads = QSpinBox(self)
        self.spinbox_threads.move(570, 500)
        self.spinbox_threads.resize(81, 31)
        self.spinbox_threads.setFont(QFont('Arial', 10))
        self.spinbox_threads.setMaximum(9999)
        self.spinbox_threads.setValue(2)

        self.label_progressbar = QLabel(self)
        self.label_progressbar.move(140, 530)
        self.label_progressbar.resize(91, 21)
        self.label_progressbar.setFont(QFont('Arial', 10))
        self.label_progressbar.setText('Progress bar')

        self.progressbar = QProgressBar(self)
        self.progressbar.move(10, 550)
        self.progressbar.resize(361, 41)
        self.progressbar.setFont(QFont('Arial', 10))
        self.progressbar.setMaximum(100)

        self.button_start = QPushButton('Start', self)
        self.button_start.move(380, 550)
        self.button_start.resize(131, 41)
        self.button_start.clicked.connect(self.start)

        self.button_stop = QPushButton('Stop', self)
        self.button_stop.move(550, 550)
        self.button_stop.resize(131, 41)
        self.button_stop.clicked.connect(self.close)

        self.browser_widget = QWidget(self)
        self.browser_widget.move(0, 0)
        self.browser_widget.resize(371, 591)

        self.user_agent = QWebEngineView(self.browser_widget)
        self.user_agent.load(QUrl('https://www.ipchicken.com/'))
        self.user_agent.resize(371, 521)
        self.user_agent.setZoomFactor(0.62)
        self.user_agent.show()

        self.counter = []
        self.timer = QTimer()

    def start(self):
        delay = self.spinbox_delay.value()
        threads = self.spinbox_threads.value()
        links = self.text_links.toPlainText().splitlines()

        if self.checkbox_proxy.isChecked():
            proxies = self.text_proxy.toPlainText().splitlines()
            if not proxies:
                QMessageBox.about(self, 'Error', 'Proxy is absent')
                return

        if not links:
            QMessageBox.about(self, 'Error', 'Link(s) is absent')
            return

        for link in links:
            if not link.startswith('http'):
                link = r'https://' + link

            for thread in range(threads):

                if 'proxies' in locals():
                    pr = random.choice(proxies)
                else:
                    pr = None

                br = random.choice(list(BROWSER.values()))
                os = random.choice(list(OS.values()))

                if self.checkbox_os_browser.isChecked():
                    useragent = f"user-agent=Mozilla/5.0 ({os}) {br}"
                else:
                    useragent = None

                self.progressbar.setValue(int((thread + 1) / (threads * len(links)) * 100))

                self.counter.append(QMainWindow())
                self.ui = Ui_SecondWindow()
                self.ui.setupUi(self.counter[-1], link, useragent, pr)
                self.counter[-1].show()

                # QApplication.processEvents()

                # self.timer.startTimer(3000)
                time.sleep(1)

    def stop(self):
        QMessageBox.about(self, 'Stop', f"Process was broken")
        return


if __name__ == '__main__':
    app = QApplication(sys.argv)  # New QApplication
    app.setStyle('Fusion')
    window = MainWindow()  # Create new interface
    window.show()  # show interface
    app.exec_()  # start of application

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question