您现在的位置是:首页 >学无止境 >爬虫小项目-worktime网站首页学无止境

爬虫小项目-worktime

追风zht 2024-06-17 10:24:54
简介爬虫小项目-worktime

test.py

from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtGui import QIcon, QPalette, QBrush, QColor, QIntValidator
from selenium.webdriver import ActionChains
from selenium.webdriver.remote.webelement import WebElement

import ui_worktime
from selenium import webdriver
import os

from selenium.webdriver.common.by import By
from time import sleep
import datetime
import sys
from threading import Thread, Lock
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QTableWidgetItem, QAction, QLineEdit

option = webdriver.ChromeOptions()
option.add_argument('window-size=1920x3000')  # 指定浏览器分辨率
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument('--hide-scrollbars')  # 隐藏滚动条, 应对一些特殊页面
bor = webdriver.Chrome()

def counttime(time):
    print(time)
    tmp = time.split("-")
    start = tmp[0]  # 开始时间
    end = tmp[1]  # 结束时间
    value = int(end.split(":")[0])
    if (int(end.split(":")[0]) < 18 and int(end.split(":")[1]) > 30):
        end = '17:30'
    start = datetime.datetime.strptime(start, "%H:%M")
    end = datetime.datetime.strptime(end, "%H:%M")
    data = end - start  # 天数小时分钟
    worktime = str(data)
    if not (worktime.find('-1 day, ') == -1):
        worktime = worktime.replace('-1 day, ', '')
        print(worktime)

    tmp = worktime.split(':')
    data = int(tmp[0]) * 60 + int(tmp[1])
    if (value >= 18):
        data -= 120
    else:
        data -= 90
    return data


def isElementExist(browser, Type, element):
    try:
        if (Type == 'id'):
            var = browser.find_element(By.ID, element)
            return True
        elif (Type == 'xpath'):
            var = browser.find_element(By.XPATH, element)
            return True
        elif (Type == 'class'):
            var = browser.find_element(By.CLASS_NAME, element)
            return True
    except:
        return False


def connectshr():


    # 全屏
    bor.maximize_window()
    bor.get('https://im.hmntechnologies.com/yzj-layout/home/')
    user = bor.find_element(By.ID, 'account')
    ui.statusBar.showMessage("Connecting...")
    user.send_keys(ui.lineEdit.text())
    pwd = bor.find_element(By.ID, 'pwd')
    pwd.send_keys(ui.lineEdit_2.text())
    btn = bor.find_element(By.CLASS_NAME, 'btn-login')
    btn.click()
    var = ""
    if (isElementExist(bor, "class", 'login-tip')):
        var = bor.find_element(By.CLASS_NAME, 'login-tip').text
        print(var)
    if len(var) != 0:
        bor.close()
        bor.quit()
        ui.statusBar.showMessage('Connect Failed' + var)
    else:
        sleep(2)
        if (isElementExist(bor, 'xpath', "//div[contains(text(),'知道了')]")):
            var = bor.find_element(By.XPATH, "//div[contains(text(),'知道了')]")
            var.click()

        var = bor.find_element(By.XPATH, "//*[@id='home-container']/iframe")
        bor.switch_to.frame(var)

        if (isElementExist(bor, 'xpath',
                           "/html/body/div/div/div/div/div[2]/div/div/div/div[11]/div/div/div/div[2]/div/div/div/div/div/div/div/div/div/div/div[1]/div[2]/div")):
            var = bor.find_element(By.XPATH,
                                   "/html/body/div/div/div/div/div[2]/div/div/div/div[11]/div/div/div/div[2]/div/div/div/div/div/div/div/div/div/div/div[1]/div[2]/div")
            var.click()
        sleep(2)
        handles = bor.window_handles  # 获取当前浏览器的所有窗口句柄
        bor.switch_to.window(handles[-1])  # 切换到最新打开的窗口
        if (isElementExist(bor, 'xpath',
                           "/html/body/div[2]/div[2]/div[2]/button")):
            var = bor.find_element(By.XPATH,
                                   "/html/body/div[2]/div[2]/div[2]/button")
            var.click()
        sleep(2)

        bor.get("https://hmn.kdeascloud.com/shr/dynamic.do?uipk=com.kingdee.eas.hr.ats.app.WorkCalendar.empATSDeskTop&inFrame=true&fromHeader=true&serviceId=rgS9VQrXSjmYmCSHX7QB2vI9KRA%3D")
        handles = bor.window_handles  # 获取当前浏览器的所有窗口句柄
        bor.switch_to.window(handles[-1])  # 切换到最新打开的窗口
        ui.pushButton.setEnabled(0)
        ui.lineEdit.setEnabled(0)
        ui.lineEdit_2.setEnabled(0)
        ui.pushButton_2.setEnabled(1)
        ui.statusBar.showMessage('Connect  Succeeded')

def querydata():
    ui.statusBar.showMessage('Querying...')
    ui.tableWidget.clear()
    sleep(1)
    try:

        var = ui.comboBox.currentText()
        var = var.replace("年", '')
        num = int(var)
        var = bor.find_element(By.XPATH,
                               "/html/body/div[4]/div/div/div/div/div[3]/div/div[1]/div[1]/div[1]")
        var = var.text
        var = var[:4]
        print(var)
        while (num < int(var)):
            bor.find_element(By.XPATH, '//*[@id="monthSelector"]/i').click()
            sleep(1)
            var = bor.find_element(By.XPATH,
                                   "/html/body/div[4]/div/div/div/div/div[3]/div/div[1]/div[1]/div[1]").text
            var = var[:4]
            print(var)
        while (num > int(var)):
            bor.find_element(By.XPATH, '//*[@id="monthSelector"]/i[2]').click()
            sleep(1)
            var = bor.find_element(By.XPATH,
                                   "/html/body/div[4]/div/div/div/div/div[3]/div/div[1]/div[1]/div[1]").text
            var = var[:4]
            print(var)
        var = ui.comboBox_2.currentText()
        var = var.replace("月", '')
        num = int(var)

        var = bor.find_element(By.XPATH,
                               "/html/body/div[4]/div/div/div/div/div[3]/div/div[1]/div[1]/div[2]/div").text
        while (num < int(var)):
            bor.find_element(By.XPATH, '//*[@id="monthSelector"]/i').click()
            sleep(1)
            num += 1
        while (num > int(var)):
            bor.find_element(By.XPATH, '//*[@id="monthSelector"]/i[2]').click()
            sleep(1)
            num -= 1
        sleep(1)
        lst = []
        lst = bor.find_elements(By.CLASS_NAME,"planWorkTime")

        lst1 = []
        count = 0
        for r in lst:
            r.click()
            sleep(0.5)
            ui.tableWidget.viewport().update()
            var1 =bor.find_element(By.XPATH,
                               "/html/body/div[4]/div/div/div/div/div[3]/div/div[2]/div[4]/div[1]/div[2]").text
            var4 = ui.comboBox_2.currentText() + str(count + 1) + '日'

            if count > 16:
                ui.tableWidget.setItem(count - 17, 4, QTableWidgetItem(var1))
                ui.tableWidget.setItem(count - 17, 3, QTableWidgetItem(var4))
            else:
                ui.tableWidget.setItem(count, 1, QTableWidgetItem(var1))
                ui.tableWidget.setItem(count, 0, QTableWidgetItem(var4))

            if not (var1 == '' or var1 == '无出勤记录'):
                var3 = counttime(var1)
                var3 = str(var3 // 60) + '小时' + str(var3 - (var3 // 60) * 60) + '分钟'

                if (bor.find_element(By.XPATH, "/html/body/div[4]/div/div/div/div/div[3]/div/div[2]/div[7]/div[1]").text == ''):
                    if (counttime(var1) > 120):
                        lst1.append(counttime(var1))
                    else:
                        var3 = '无效工时' + var3
                else:
                    var3 = '加班' + var3
                if count > 16:
                    ui.tableWidget.setItem(count - 17, 5, QTableWidgetItem(str(var3)))
                else:
                    ui.tableWidget.setItem(count, 2, QTableWidgetItem(str(var3)))
            else:
                if count > 16:
                    ui.tableWidget.setItem(count - 17, 5, QTableWidgetItem(var1))
                else:
                    ui.tableWidget.setItem(count, 2, QTableWidgetItem(var1))
            count += 1
        print(len(lst1))
        print(lst1)
        sum = 0
        ui.lineEdit_3.clear()
        for l in lst1:
            sum = l + sum
        if (sum // 60 - len(lst1) * 8) < 0:
            ui.lineEdit_3.setText(
                '工时' + str(sum // 60) + '小时' + str(sum - (sum // 60) * 60) + '分钟' + '   ' + '应当工作' + str(
                    len(lst1) * 8) + '小时' + '  ' + '工时余量' + str(sum // 60 - len(lst1) * 8 + 1) + '小时' + str(
                    sum - (sum // 60) * 60 - 60) + '分钟')
        else:
            ui.lineEdit_3.setText(
                '工时' + str(sum // 60) + '小时' + str(sum - (sum // 60) * 60) + '分钟' + '   ' + '应当工作' + str(
                    len(lst1) * 8) + '小时' + '  ' + '工时余量' + str(sum // 60 - len(lst1) * 8) + '小时' + str(
                    sum - (sum // 60) * 60) + '分钟')

        print('工时' + str(sum // 60) + '小时' + str(sum - (sum // 60) * 60) + '分钟')
        print('应当工作' + str(len(lst1) * 8) + '小时')
        print('工时余量' + str(sum // 60 - len(lst1) * 8) + '小时' + str(sum - (sum // 60) * 60) + '分钟')
        ui.tableWidget.setHorizontalHeaderLabels(['日期', '有效出勤', '工作时间', '日期', '有效出勤', '工作时间'])
        lst.clear()
        lst1.clear()
        ui.statusBar.showMessage('Query  OK')
    except:

        print("异常")

        ui.statusBar.showMessage('Query  Error')


# 主窗体对象调用setupUi方法,对QMainWindow对象进行设置
def closedrive():
    try:
        print('123')
        handles = bor.window_handles  # 获取当前浏览器的所有窗口句柄
        for handle in handles:
            bor.switch_to.window(handle)
            bor.close()
        bor.quit()

    except:
        print('err')


def about1():
    print('11')

    QMessageBox.about(w, '关于', '余量工时计算工具 v1.1')


if __name__ == '__main__':
    global w, ui, App

    # 创建线程01,不指定参数

    App = QApplication(sys.argv)  # 创建QApplication对象,作为GUI主程序入口
    ui = ui_worktime.Ui_mainWindow()  # 创建主窗体对象
    w = ui_worktime.MyWidget()  # 实例化QMainWindow类
    ui.setupUi(w)
    w.setbor(bor)
    w.setWindowIcon(QIcon("favicon.ico"))
    w.setFixedSize(620, 720)

    # qss = "QWidget#mainWindow{border-image:url(下载.bmp);}"
    # w.setStyleSheet( qss )
    ui.tableWidget.setColumnCount(6)
    ui.tableWidget.setRowCount(17)
    ui.tableWidget.verticalHeader().setVisible(0)
    # ui.tableWidget.setStyleSheet( "background-color:rgba(0,0,0,0)" );
    ui.actionabout.triggered.connect(about1)
    ui.menubar.addAction(ui.actionabout)
    ui.tableWidget.setHorizontalHeaderLabels(['日期', '有效出勤', '工作时间', '日期', '有效出勤', '工作时间'])
    ui.tableWidget.horizontalHeader().setHighlightSections(True)
    ui.pushButton_2.setEnabled(0)
    ui.pushButton.clicked.connect(connectshr)
    ui.pushButton_2.clicked.connect(querydata)

    w.show()
    # App.exit()
    App.exec_() # 循环中等待退出程序
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。