imgfile_main_client.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import sys
  2. from PyQt5 import QtWidgets, QtCore, QtGui
  3. from PyQt5.QtWidgets import QMessageBox
  4. from file_spider import imgfile_main_by_mongodb_with_thread_no_proxy
  5. class MyWindow(QtWidgets.QWidget):
  6. def __init__(self):
  7. super(MyWindow, self).__init__()
  8. self.resize(500, 100)
  9. self.setWindowTitle("Mouser爬虫 - 器件Logo")
  10. self.worker = MyWorker()
  11. _translate = QtCore.QCoreApplication.translate
  12. self.label = QtWidgets.QLabel(self)
  13. self.label.setObjectName("label")
  14. self.label.setText(_translate("Dialog", "<span style=\" font-weight:600;\">请输入账号</span>"))
  15. self.label.setFixedWidth(80)
  16. # 用户名输入框
  17. self.nameEdit = QtWidgets.QLineEdit(self)
  18. self.nameEdit.setObjectName("nameEdit")
  19. self.threadLabel = QtWidgets.QLabel(self)
  20. self.threadLabel.setObjectName("threadLabel")
  21. self.threadLabel.setText(_translate("Dialog", "<span style=\" font-weight:600;\">线程数</span>"))
  22. self.threadLabel.setFixedWidth(50)
  23. # 线程输入框
  24. self.threadEdit = QtWidgets.QLineEdit(self)
  25. self.threadEdit.setObjectName("threadEdit")
  26. self.threadEdit.setFixedWidth(50)
  27. self.threadEdit.setText('100')
  28. # 开始按钮
  29. self.startButton = QtWidgets.QPushButton(self)
  30. self.startButton.setObjectName("startButton")
  31. self.startButton.setText("开始")
  32. self.startButton.setFixedWidth(70)
  33. self.startButton.clicked.connect(self.start)
  34. # 取消按钮
  35. self.cancelButton = QtWidgets.QPushButton(self)
  36. self.cancelButton.setObjectName("cancelButton")
  37. self.cancelButton.setText("取消")
  38. self.cancelButton.setFixedWidth(70)
  39. self.cancelButton.clicked.connect(self.close)
  40. # 状态统计
  41. self.statLabel = QtWidgets.QLabel(self)
  42. self.statLabel.setObjectName("statLabel")
  43. # 布局
  44. self.grid = QtWidgets.QGridLayout(self)
  45. self.grid.addWidget(self.label, 1, 0)
  46. self.grid.addWidget(self.nameEdit, 1, 1)
  47. self.grid.addWidget(self.threadLabel, 1, 2)
  48. self.grid.addWidget(self.threadEdit, 1, 3)
  49. self.grid.addWidget(self.startButton, 1, 4)
  50. self.grid.addWidget(self.cancelButton, 1, 5)
  51. self.grid.addWidget(self.statLabel, 2, 0, 1, 6)
  52. # 信号
  53. self.worker.stat_signal.connect(self._update_stat)
  54. def _update_stat(self, succeed, failured, active, total):
  55. self.statLabel.setText("成功 %s,失败 %s,正在爬取 %s" % (succeed, failured, active))
  56. def start(self):
  57. name = self.nameEdit.text()
  58. thread = self.threadEdit.text()
  59. if len(name) == 0:
  60. QMessageBox.information(self, "提示", "请先输入您的账号,输入不存在的账号会自动创建")
  61. elif len(thread) == 0:
  62. QMessageBox.information(self, "提示", "请先输入您开启的线程数,比如100")
  63. else:
  64. self.nameEdit.setDisabled(True)
  65. self.threadEdit.setDisabled(True)
  66. self.startButton.setDisabled(True)
  67. self.worker.setName(name)
  68. self.worker.setThreadSize(int(thread))
  69. try:
  70. self.worker.start()
  71. except:
  72. None
  73. class MyWorker(QtCore.QThread):
  74. stat_signal = QtCore.pyqtSignal(int, int, int, int)
  75. def __init__(self, userName=None, threadSize=100):
  76. super(MyWorker, self).__init__()
  77. self.userName = userName
  78. self.threadSize = threadSize
  79. def setName(self, userName):
  80. self.userName = userName
  81. def setThreadSize(self, threadSize):
  82. self.threadSize = threadSize
  83. def run(self):
  84. spider = imgfile_main_by_mongodb_with_thread_no_proxy.ImgFileMain(userName=self.userName, maxThread=self.threadSize)
  85. while spider.hasNext():
  86. spider.craw()
  87. self.stat_signal.emit(*spider.statistic())
  88. if __name__ == "__main__":
  89. app = QtWidgets.QApplication(sys.argv)
  90. myshow = MyWindow()
  91. myshow.show()
  92. sys.exit(app.exec_())