After i click the button, the form become unresponsive until the parsing function finish its work.
I'd like to move searchAll function to thread. I did read several answers to similar questions, but i didn't understand how.
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.buttonOK.clicked.connect(self.searchAll)
self.show()
def searchAll(self):
sID = self.ui.txtSellerID.text()
sUrl = "https://removed.com/" + sID + "/p/?section=2&page=1"
sr = requests.get(sUrl)
soup1 = BeautifulSoup(sr.text, "html.parser")
NumberOfPagesBlock = soup1.find_all("li", class_="text-gray")
if not NumberOfPagesBlock:
QMessageBox.about(self, "Warning", "Nothing Here")
else:
items = re.compile(r'[^\d.]+')
PagesCount = -(-items // 60)
for i in range(1, int(PagesCount + 1)):
itemsIdDs = soup1.find_all("div", class_="large-single-item")
for itemsIdD in itemsIdDs:
iUrl = ("https://removed.com/" + itemsIdDs.get('data-ean') + "/s")
r = requests.get(iUrl)
soup = BeautifulSoup(r.text, "html.parser")
seller = soup.find("div", id="productTrackingParams")
title = (str(ctr) + '- ' + "Title " + str(seller.get('data-title')))
self.ui.txtDetails.appendPlainText(title)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
You have to execute the heavy task (requests + BeautifulSoup) in another thread since they block the main thread where the GUI lives, preventing the GUI from working correctly and this is manifested, for example, by freezing the screen. In this case I will implement a worker-thread approach: