单击按钮后,表单将变得无响应,直到解析函数完成其工作。你知道吗
我想把searchAll函数移到线程。我确实读过几个类似问题的答案,但我不明白怎么读。你知道吗
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_())
您必须在另一个线程中执行繁重的任务(requests+BeautifulSoup),因为它们会阻塞GUI所在的主线程,从而阻止GUI正常工作,例如,冻结屏幕。在本例中,我将实现工作线程方法:
相关问题 更多 >
编程相关推荐