1.环境安装: 

  1.安装pyQt5 pip3 install pyQt5  
  2.安装设计器 pip3 install pyQt5-tools  (英文版的) 我是用的是自己Windows上安装的qt-unified软件
  3.qt-unified安装,下载地址 https://www.qt.io/
 

2.qt-unified软件设计界面

  新建项目—>Qt—>From

  

  

  根据左边菜单栏上的控件直接拖到窗口上布局 设计如下界面:

   

  保存文件,命令行切换到保存的.ui文件目录执行如下命令将.ui文件转化py文件

  

pyuic5 -o mainwindow.py mainwindow.ui

  其中,-o 后的参数为输出文件的名称 -o 后第二个参数即为生成的ui文件的名称,报错请注意环境变量

  这个时候运行会报错,所以还需要配置几个步骤。

  • 增加几个引用
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_())

  

  然后成功运行,弹出界面。

  这个时候,我们要把按钮和输入框的改造下,使其能够交互起来。  

  • QPushButton的信号槽连接:self.choose_file.clicked.connect(self.open_file),传入的是点击后执行的方法。

  • 获取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_())

  

最新文章

  1. HTML 上传图片实用小技巧
  2. ABP使用及框架解析系列 - [Unit of Work part.1-概念及使用]
  3. js 获取浏览器高度和宽度值(多浏览器)
  4. 用sql从一张表更新数据到另外一张表(多表数据迁移)
  5. 把input类型剔出来
  6. Struts2的工作流程
  7. Android开发第2篇 - Git插件安装
  8. HDU-2521 反素数
  9. NET基础课--WinForm开发推荐3
  10. 转发年浩大神的spfa算法
  11. OCA读书笔记(13) - 性能管理
  12. 面试必问的 volatile,你了解多少?
  13. c#实战开发:以太坊Geth 命令发布智能合约 (五)
  14. linux 添加ssh和开启ssh服务apt管理的ubuntu
  15. CCF CSP 201712-1 最小差值
  16. Cognos集成至portal平台查看报表报错RSV-BBP-0022
  17. 小程序学习笔记三:页面文件详解之视图层WXML、WXS、WXSS文件
  18. python运算符,数据类型,数据类型操作,三目运算,深浅拷贝
  19. 尝试解决cifar10问题
  20. UVALive - 3266 (贪心) 田忌赛马

热门文章

  1. js车牌号校验
  2. 【WEB基础】HTML &amp; CSS 基础入门(9)CSS盒子
  3. 关于SQL Server数据库中的标识列
  4. 《web-Mail服务的搭建》
  5. KendoUI 自定义验证:
  6. linux解压与参数介绍
  7. Backbone源码风格
  8. 《Head First 设计模式》之装饰者模式——饮料加工
  9. 使用data-自定义数据及如何获取该值
  10. C#开发android应用实战 源码