E
E
eellazy2021-03-23 12:32:49
Python
eellazy, 2021-03-23 12:32:49

How to fix QThread: Destroyed while thread is still running error?

Good afternoon!)
When the program is running, I get the following error
QThread: Destroyed while thread is still running

. My program code:

class ParserController:
    def __init__(self, model, view):
        self._model = model
        self._view = view

    def query_add(self, data):
        if self._model.insert_data(data):
            print('Запись добавлена в базу')
        else:
            print('Запись НЕ добавлена в базу')

    def start_parse_autovia(self):
        self.thread_1 = QtCore.QThread()
        self.autovia_thread = ParseAutovia()
        self.autovia_thread.moveToThread(self.thread_1)
        self.thread_1.started.connect(self.autovia_thread.run)
        self.autovia_thread.query.connect(self.query_add)
        self.autovia_thread.finished.connect(self.thread_1.quit)
        self.thread_1.start()


class ParseAutovia(QtCore.QObject):
    finished = QtCore.pyqtSignal()
    query = QtCore.pyqtSignal(dict)

    START_PAGE = 'https://www.autovia.sk/osobne-auta'
    PAGINATION_PAGE = 'https://www.autovia.sk/osobne-auta/?p[page]='

    def get_data(self, html):
        # Get title
        try:
            title = html.find('h1').text.strip()
        except:
            title = ''

        # Get phone
        try:
            phone = html.find('div', class_='resp-buttons').find_all('a')[0]['href']
        except:
            phone = ''

        # Get name
        try:
            name = html.find('div', class_='resp-contact-top').find('span', class_='resp-subject').text.strip()
        except:
            name = ''

        # Get location
        try:
            location = html.find('div', class_='resp-contact-bottom').find('div', class_='resp-location').find('span').text.strip()
        except:
            location = ''

        # Get date
        try:
            date = html.find('div', class_='resp-meta').find_all('div', class_='col-6')[0].text.strip()
            date = date.replace('Aktualizované: ', '')
        except:
            date = ''

        # Get login
        try:  # Логин продавца
            login = html.find('div', class_='resp-contact-top').find('a', class_='resp-subject').text.strip()
        except:
            login = ''

        # Get login link
        try:
            login_link = html.find('div', class_='resp-contact-top').find('a', class_='resp-subject')['href']
        except:
            login_link = ''

        # Get page link
        try:
            page_link = html.find('meta', attrs={'property': 'og:url'})['content']
        except:
            page_link = ''

        data = {
            'title': title,
            'phone': phone,
            'name': name,
            'location': location,
            'date': date,
            'login': login,
            'login_link': login_link,
            'page_link': page_link
        }

        return data

    def get_html(self, url):
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'lxml')
        return soup

    def get_pagination_count(self):
        soup = self.get_html(self.START_PAGE)
        count = soup.find('div', class_='resp-pager').find('label', class_='resp-after').text.strip()
        count = int(count.replace('z', '').replace(' ', ''))
        return count

    def get_pagination_links(self, pagination):
        url = self.PAGINATION_PAGE + str(pagination)
        soup = self.get_html(url)

        links = []
        items = soup.find('section', class_='resp-search-results').find_all('div', class_='resp-item')

        for item in items:
            link = item.find('h2').find('a').get('href')
            links.append(link)

        return links

    def run(self):
        pagination_count = self.get_pagination_count()
        pagination = 1

        while pagination <= pagination_count:
            pagination_links = self.get_pagination_links(pagination)

            for url in pagination_links:
                html = self.get_html(url)
                data = self.get_data(html)

                self.query.emit(data)
                print(data['phone'])

            pagination += 1

        self.finished.emit()


What am I doing wrong? The most interesting thing is that the thread goes through two iterations and then an error pops up.
It turns out that my stream is self-cleaning or what?

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