# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'bilibili.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


import requests
import pyttsx3.drivers
import pyttsx3.drivers.sapi5
import pyttsx3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from selenium import webdriver
import time
import re


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 350)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(350, 10, 180, 50))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(15)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(200, 100, 540, 30))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(60, 200, 240, 30))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(500, 200, 240, 30))
        self.lineEdit_3.setObjectName("lineEdit")
        self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_4.setGeometry(QtCore.QRect(200, 50, 540, 30))
        self.lineEdit_4.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(60, 100, 140, 30))
        self.label_2.setObjectName("label_2")
        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(60, 50, 140, 30))
        self.label_9.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(60, 250, 333, 30))
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(350, 200, 100, 30))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(15)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(240, 160, 500, 30))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label_4.setFont(font)
        self.label_4.setAcceptDrops(False)
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(60, 160, 190, 30))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 750, 25))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        # 点击功能
        self.pushButton.clicked.connect(self.label_3.show)
        self.pushButton.clicked.connect(self.slot)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "网页文字下载器"))
        self.label.setText(_translate("MainWindow", "网页文字下载"))
        self.lineEdit.setPlaceholderText(_translate("MainWindow", " 输入网址 "))
        self.lineEdit_2.setPlaceholderText(_translate("MainWindow", " 上边界条件 "))
        self.lineEdit_3.setPlaceholderText(_translate("MainWindow", " 下边界条件 "))
        self.lineEdit_4.setPlaceholderText(_translate("MainWindow", " 保存在当前目录下"))
        self.label_2.setText(_translate("MainWindow", "请输入网址："))
        self.label_9.setText(_translate("MainWindow", "请输入文件名："))
        self.label_3.setText(_translate("MainWindow", "正在执行》》》》"))
        self.label.setStyleSheet("color:#bb22ff")  # 设置字体颜色
        self.label_2.setStyleSheet("color:#bb00ff")
        self.label_3.setStyleSheet("color:red")
        self.label_4.setStyleSheet("color:#bb00ff")
        self.label_5.setStyleSheet("color:#bb00ff")
        self.label_9.setStyleSheet("color:#bb22ff")
        self.label_3.hide()
        self.pushButton.setText(_translate("MainWindow", "执行"))
        self.pushButton.setStyleSheet("color:red")
        self.label_4.setText(_translate("MainWindow", "http://www.ruiwen.com/ziwojianding/2528066.html"))
        self.label_4.setTextInteractionFlags(Qt.TextSelectableByMouse)  # 设置可复制
        self.label_5.setText(_translate("MainWindow", "测试网址（可复制）："))

    # 警告框
    def messageDialog(self):
        msg_box = QMessageBox(QMessageBox.Warning, '提示', '请输入房间号    ')
        msg_box.exec_()

    def slot(self):
        self.label_3.setText("执行中》》》》")
        print('正在运行')
        url = str(self.lineEdit.text())  # 获取输入·框的值self.lineEdit.text()
        if url == '':
            print('未输入')
            self.label_3.setText("请重新输入网址~")
            self.messageDialog()
        else:
            print(url)
            options = webdriver.ChromeOptions()
            options.headless = True  # 设置选项为无界面
            options.add_argument(
                'user-agent="Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"')
            driver = webdriver.Chrome(options=options)
            # driver.get("http://www.ruiwen.com/ziwojianding/2528066.html")
            driver.get(url)
            time.sleep(2)
            bj1 = str(self.lineEdit_2.text())  # 获取输入·框的值self.lineEdit.text()
            # bj1 = '毕业自我鉴定1'
            bj2 = str(self.lineEdit_2.text())  # 获取输入·框的值self.lineEdit.text()
            # bj2 = '毕业自我鉴定2'
            filename = str(self.lineEdit_4.text())
            # print(driver.page_source)#打印页面源码
            pattern = re.compile(r"%s(.*?)%s" % (bj1, bj2))
            result = pattern.findall(driver.page_source)
            pattern = re.compile('<p>(.*?)</p>', re.S)
            result = pattern.findall(result[0])
            print(''.join(result))
            with open('%s.txt' % filename, 'w') as f:
                for index, item in enumerate(result):
                    if (index + 1) % 11 == 0: f.write('\n')
                    f.write('' + item)
            driver.quit()
            self.label_3.setText("执行完成")


import sys  # 导入系统模块


def show_MainWindow():
    app = QtWidgets.QApplication(sys.argv)  # 实例化QApplication类，作为GUI主程序入口
    MainWindow = QtWidgets.QMainWindow()  # 创建MainWindow
    ui = Ui_MainWindow()  # 实例化ui类
    ui.setupUi(MainWindow)  # 设置窗口UI
    MainWindow.show()  # 显示窗口
    sys.exit(app.exec_())  # 当窗口创建完成时，需要结束主循环过程


if __name__ == "__main__":
    show_MainWindow()
