Hello. I wrote a simple parser:
import codecs #Переопределяю кодировку def open(path, mode): return codecs.open(path, mode, 'utf-8') import time from bs4 import BeautifulSoup # Импортирую парсер import requests from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent from PyQt5.QtCore import QUrl, QFileInfo audioFile = r"C:\Users\Grzegorz\PycharmProjects\parser\parser\alert.mp3" player = QMediaPlayer() player.setMedia(QMediaContent(QUrl.fromLocalFile(QFileInfo(audioFile).absoluteFilePath()))) # Для оповещения def parser (site): response = requests.get(site) soup = BeautifulSoup(response.content, "html.parser") # Выгружаю содержимое content = soup.find('div', {"class": "content"}) for row in content.find_all('div', {"class": "entry"}): title = row.find_all('h3', {'class': "entry__title"}) description = row.find_all('div', {"class": "entry__content"})[0].text[:-7] link_beta = str(row.find_all('h3', {'class': "entry__title"}))[35:] a = link_beta.index('"') link = link_beta[:a] photo = row.find('img', {'class': 'wp-post-image'}).attrs["src"] full = (photo[2:] + '\n' + '🌎 ' + title[0].text + '\n' + '📎 ' + description + '\n' + '📌 ' + link + '\n\n') result1 = open("fly4free.txt", "r").readlines() check = str(result1).find(full[:50]) # Проверяю файл на наличие спарсенного текста if check == -1: result1.insert(0, full) result = open("fly4free.txt", "w") for line in result1: result.write(line) result.close() player.play() while True: # Псевдо-автоматизация скрипта parser("http://www.fly4free.pl") parser("http://www.fly4free.com/flights/flight-deals/europe/") time.sleep(5) But for some reason, when you start via cmd, you get an error: "QObject :: startTimer: Timers can only be used with threads started with QThread"
Can you please tell me how to fix it?
Thank.
Qtobjects to work correctly, you must always createQApplicationand only after that create the remaining objects. Also,Qtobjects will not work unlessEventLooprunning. That is, message processing will not work for you until you execute theQApplication.execmethod. When using Qt, you will not be able to usewhile Trueandtime.sleep. - Avernialtime.sleep, your entire application will freeze for 5 seconds, including playing the sound. Therefore, instead of time.sleep you should useQTimer. Andwhile Truewill be replaced withQApplication.execorQCoreApplication. - Avernial