新建項目—>Qt—>Fromhtml
根據左邊菜單欄上的控件直接拖到窗口上佈局 設計以下界面:python
保存文件,命令行切換到保存的.ui文件目錄執行以下命令將.ui文件轉化py文件express
pyuic5 -o mainwindow.py mainwindow.ui
其中,-o 後的參數爲輸出文件的名稱 -o 後第二個參數即爲生成的ui文件的名稱,報錯請注意環境變量json
這個時候運行會報錯,因此還須要配置幾個步驟。api
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog,QMessageBox import sys
if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
而後成功運行,彈出界面。cookie
這個時候,咱們要把按鈕和輸入框的改造下,使其可以交互起來。 session
QPushButton的信號槽鏈接:self.choose_file.clicked.connect(self.open_file)
,傳入的是點擊後執行的方法。app
獲取QLineEdit裏面的文本:self.cookie_edit.text()
。函數
將文案輸出到QTextBrowser組件中顯示:self.message_show.append('爬取所有文章成功\n')
。佈局
判斷複選框是否選中self.only_name.isChecked()
,返回的是true/false。
運行中我發現,每次執行的時候,QTextBrowser組件中的文案,不能像print()方法同樣,一行一行的輸出,每次都是要等到所有執行完後,一次性輸出。這個不符合我設計QTextBrowser組件的初衷。因而我又去問了問怎麼樣才能讓QTextBrowser組件中的文案一行一行的輸出。
最終多方嘗試後,解決了這個問題,就是加入這句代碼QApplication.processEvents()
,能夠將這句代碼放入循環中,就能作到界面的實時刷新了。
在複雜操做的過程當中頻繁調用QApplication.processEvents()。這個函數告訴Qt處理全部那些尚未被處理的各種事件,而後將控制權返還給調用者。
實際上QApplication.exec()就是一個不停調用processEvents()函數的while循環。
我的示例代碼
# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog,QMessageBox import sys class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(479, 547) self.textout = QtWidgets.QTextBrowser(Form) self.textout.setGeometry(QtCore.QRect(20, 251, 431, 271)) self.textout.setObjectName("textout") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(180, 10, 101, 31)) self.label.setObjectName("label") self.lujinuot = QtWidgets.QLabel(Form) self.lujinuot.setGeometry(QtCore.QRect(90, 60, 271, 20)) self.lujinuot.setObjectName("lujinuot") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(20, 60, 71, 21)) self.label_3.setObjectName("label_3") self.selectfile = QtWidgets.QPushButton(Form) self.selectfile.setGeometry(QtCore.QRect(370, 60, 81, 23)) self.selectfile.setObjectName("selectfile") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(20, 110, 54, 12)) self.label_4.setObjectName("label_4") self.incookie = QtWidgets.QLineEdit(Form) self.incookie.setGeometry(QtCore.QRect(70, 109, 381, 21)) self.incookie.setObjectName("incookie") self.startpachong = QtWidgets.QPushButton(Form) self.startpachong.setGeometry(QtCore.QRect(20, 190, 431, 41)) self.startpachong.setObjectName("startpachong") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "爬蟲程序")) self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:18pt;\">程序設置</span></p></body></html>")) self.lujinuot.setText(_translate("Form", " ")) self.label_3.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:12pt;\">文件路徑</span></p></body></html>")) self.selectfile.setText(_translate("Form", "選擇文件")) self.label_4.setText(_translate("Form", "cookie:")) self.startpachong.setText(_translate("Form", "開始爬取")) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_Form() ui.setupUi(MainWindow) MainWindow.show() #能夠經過QFileDialog.getOpenFileName打開文件目錄,選擇文件 #能夠經過QFileDialog.getExistingDirectory選擇文件目 # ui.cookie_edit.text() #獲取QLineEdit裏面的文本 # ui.lujinuot.setText(filename) #設置QLineEdit裏面的文本 #QtWidgets.QApplication.processEvents() #界面實時刷新 #ui.selectfile.clicked.connect(selectfile) #點擊事件 # ui.textout.append("點擊了選擇文件按鈕\n") #添加內容 # ui.selectfile.isChecked() #返回按鈕的狀態True/False #提示框 # msg_box = QMessageBox(QMessageBox.Warning, "消息提示", "消息內容") # msg_box.show() # msg_box.exec_() def selectfile(): # msg_box = QMessageBox(QMessageBox.Warning, "消息提示", "消息內容") # msg_box.show() # msg_box.exec_() filename= QFileDialog.getExistingDirectory() ui.lujinuot.setText(filename) ui.selectfile.clicked.connect(selectfile) ui.textout.append("輸入cookie點開始爬取\n") QtWidgets.QApplication.processEvents() #界面實時刷新 sys.exit(app.exec_())
實例2:
#coding:utf-8 import requests as req import time,json,os,sys import xlwt from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog,QMessageBox,QApplication import sys from multiprocessing import Process class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(479, 547) self.textout = QtWidgets.QTextBrowser(Form) self.textout.setGeometry(QtCore.QRect(20, 251, 431, 271)) self.textout.setObjectName("textout") self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(180, 20, 101, 31)) self.label.setObjectName("label") self.lujinuot = QtWidgets.QLabel(Form) self.lujinuot.setGeometry(QtCore.QRect(90, 70, 271, 20)) self.lujinuot.setObjectName("lujinuot") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(20, 70, 71, 21)) self.label_3.setObjectName("label_3") self.selectfile = QtWidgets.QPushButton(Form) self.selectfile.setGeometry(QtCore.QRect(370, 70, 81, 23)) self.selectfile.setObjectName("selectfile") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(20, 120, 54, 12)) self.label_4.setObjectName("label_4") self.incookie = QtWidgets.QLineEdit(Form) self.incookie.setGeometry(QtCore.QRect(70, 120, 381, 21)) self.incookie.setObjectName("incookie") self.startpachong = QtWidgets.QPushButton(Form) self.startpachong.setGeometry(QtCore.QRect(20, 190, 431, 41)) self.startpachong.setObjectName("startpachong") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "拼多多資料採集程序 v1.0")) self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:18pt;\">程序設置</span></p></body></html>")) self.lujinuot.setText(_translate("Form", "路徑顯示")) self.label_3.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:12pt;\">保存路徑</span></p></body></html>")) self.selectfile.setText(_translate("Form", "選擇文件")) self.label_4.setText(_translate("Form", "cookie:")) self.startpachong.setText(_translate("Form", "開始")) def writeWFH(headers): filename = "未發貨-"+str(time.strftime("%Y-%m-%d").strip())+".xls" # filename = os.path.join(ui.lujinuot.text(),filename) wbk = xlwt.Workbook() ws = wbk.add_sheet('sheet 1') ws.write(0,0,"暱稱") ws.write(0,1,"電話號碼") ws.write(0,2,"地址") ws.write(0,3,"快遞公司") ws.write(0,4,"快遞單號") count = 1 page = 1 while True: url = "https://st.huanleguang.com/api/trades?status=paid&print_status=all&shipping_delay=all&shop_id=&memo_type=1&seller_flag=-1&refund_status=all&order_type=all&area_info=all&order_num=all&post_type=all&merge_type=all&trade_type=all&page_size=200&page_no=%s&tid=&buyer_nick=&express_no=&receiver_mobile=&receiver_name=&item_name_id=&sku_size=&sku_color="%page ret = req.get(url=url,headers=headers) if len(json.loads(ret.text))>0: for line in json.loads(ret.text): TextWrite("第 %s 條記錄"%count) line = line[0] nickname = line["receiver_name"] phone = line["receiver_mobile"] add = line["receiver_state"]+line["receiver_city"]+line["receiver_district"]+line["receiver_address"] kuaidi = line["express_name"] danhao = line["express_no"] ws.write(count,0,nickname) ws.write(count,1,phone) ws.write(count,2,add) ws.write(count,3,kuaidi) ws.write(count,4,danhao) count = count+1 page = page+1 else: wbk.save(filename) TextWrite("保存成功!%s"%filename) break return def writeYFH(headers): wbk = xlwt.Workbook() ws = wbk.add_sheet('sheet 1') ws.write(0,0,"暱稱") ws.write(0,1,"電話號碼") ws.write(0,2,"地址") ws.write(0,3,"快遞公司") ws.write(0,4,"快遞單號") filename = "已發貨-%s.xls"%str(time.strftime("%Y-%m-%d").strip()) # filename = os.path.join(ui.lujinuot.text(),filename) count = 1 page = 1 while True: url = "https://st.huanleguang.com/api/trades?status=shipped&print_status=all&shipping_delay=all&shop_id=&memo_type=1&seller_flag=-1&refund_status=all&order_type=all&area_info=all&order_num=all&post_type=all&merge_type=all&trade_type=all&page_size=200&page_no=%s&tid=&buyer_nick=&express_no=&receiver_mobile=&receiver_name=&item_name_id=&sku_size=&sku_color="%page ret = req.get(url=url,headers=headers) if len(json.loads(ret.text))>0: for line in json.loads(ret.text): TextWrite("第 %s 條記錄"%count) line = line[0] nickname = line["receiver_name"] phone = line["receiver_mobile"] add = line["receiver_state"]+line["receiver_city"]+line["receiver_district"]+line["receiver_address"] kuaidi = line["express_name"] danhao = line["express_no"] ws.write(count,0,nickname) ws.write(count,1,phone) ws.write(count,2,add) ws.write(count,3,kuaidi) ws.write(count,4,danhao) count = count+1 page = page+1 else: wbk.save(filename) TextWrite("保存成功!%s"%filename) return def SelectFile(): filename = QFileDialog.getExistingDirectory() ui.lujinuot.setText(filename) def TextWrite(msg): ui.textout.append(str(msg)) print(msg) QtWidgets.QApplication.processEvents() def pa(): cookie = ui.lujinuot.text().strip() # cookie = "__guid=227988319.2581628879631838000.1540522358017.8997; gr_user_id=b83ae182-83cd-4b7e-84b0-c22e856d1c3d; gr_session_id_84652f4a0b894385=7ef6971a-1053-47b0-bc0d-2eb931e3ed88; gr_session_id_84652f4a0b894385_7ef6971a-1053-47b0-bc0d-2eb931e3ed88=true; hlg_8_0=8-5045059-3-e695f708efb6c3e814ec42de4798554f; monitor_count=2; gr_cs1_7ef6971a-1053-47b0-bc0d-2eb931e3ed88=shop_id%3A5045059; _ati=7213780759932" headers = {"Accept-Encoding":"gzip, deflate, br","Accept-Language":"zh-CN,zh;q=0.9","Connection":"keep-alive","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36","Cookie":cookie} #所有數據 try: time1 = time.time() writeWFH(headers) writeYFH(headers) time2 = time.time() time3 = time2-time1 TextWrite("用時 %s 秒"%str(int(time3*10)/10)) except Exception as e: TextWrite("訪問出錯!"+str(e)) def StartPa(): # TextWrite(ui.incookie.text()) if ui.lujinuot.text() == "路徑顯示": TextWrite("<html><head/><body><span style=\" color:red;\">請先選擇文件保存路徑!!!</span></body></html>") return elif ui.lujinuot.text() == "": TextWrite("<html><head/><body><span style=\" color:red;\">保存路徑不能爲空!!!</span></body></html>") return elif ui.incookie.text() == "": TextWrite("<html><head/><body><span style=\" color:red;\">cookie不能爲空!!!</span></body></html>") return else: pa() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_Form() ui.setupUi(MainWindow) MainWindow.show() ui.selectfile.clicked.connect(SelectFile) ui.startpachong.clicked.connect(StartPa) sys.exit(app.exec_())