# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ZhengZe.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import requests
import re
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(870, 887)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 20, 871, 71))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(24)
self.label.setFont(font)
self.label.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(40, 100, 131, 51))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(190, 100, 601, 51))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(14)
self.textEdit.setFont(font)
self.textEdit.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.IBeamCursor))
self.textEdit.setObjectName("textEdit")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 180, 151, 51))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_2.setGeometry(QtCore.QRect(190, 170, 361, 51))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(14)
self.textEdit_2.setFont(font)
self.textEdit_2.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.IBeamCursor))
self.textEdit_2.setObjectName("textEdit_2")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(0, 300, 871, 561))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(10)
self.textBrowser.setFont(font)
self.textBrowser.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.IBeamCursor))
self.textBrowser.setObjectName("textBrowser")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(570, 170, 221, 51))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(14)
self.pushButton.setFont(font)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setObjectName("pushButton")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(60, 240, 111, 41))
font = QtGui.QFont()
font.setFamily("Adobe Devanagari")
font.setPointSize(12)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.textEdit_3 = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_3.setGeometry(QtCore.QRect(190, 240, 601, 41))
self.textEdit_3.setObjectName("textEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 870, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.spider)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "正则爬虫"))
self.label.setText(_translate("MainWindow", "正则爬虫小工具"))
self.label_2.setText(_translate("MainWindow", "输入网址链接:"))
self.label_3.setText(_translate("MainWindow", "输入正则表达式:"))
self.pushButton.setText(_translate("MainWindow", "开始爬取"))
self.label_4.setText(_translate("MainWindow", "添加Cookie:"))
def spider(self):
t1 = time.perf_counter()
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'}
try:
res = requests.get(self.textEdit.toPlainText(), headers=header,cookies=self.textEdit_3.toPlainText()).text
info = re.compile(self.textEdit_2.toPlainText(), re.S).findall(res)
for i in info:
self.textBrowser.append(i)
t2=time.perf_counter()
self.label.setText("共取到"+str(len(info))+"条数据! 共用时:"+str(t2-t1)+"秒")
except Exception:
self.textBrowser.setText("爬取出现异常....")
if __name__=="__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())