Contents

今天很羞愧,写了一坨屎一样的脚本,能跑起来的拉粪车,真TM的臭,挂出来,什么时候又要写狗屎代码了就翻出来看一看,有奇效

# coding=utf-8
from win32com.client import Dispatch
from docx import Document
from selenium import webdriver
from selenium.webdriver.common import keys
# from selenium.webdriver.common.action_chains import ActionChains
import time
import os

connection=webdriver.Chrome()
connection.get("http://***.***.***.***/index.php?title="+u"首页")
connection.find_element_by_xpath('//*[@id="pt-login"]/a').click()
time.sleep(2)
username=connection.find_element_by_xpath('//*[@id="wpName1"]')
username.click()
username.send_keys("************")
password=connection.find_element_by_xpath('//*[@id="wpPassword1"]')
password.click()
password.send_keys("************")
connection.find_element_by_xpath('//*[@id="wpLoginAttempt"]').click()
connection.find_element_by_xpath('//*[@id="ca-ve-edit"]').click()
time.sleep(50)
# connection.get("http://***.***.***.***/index.php?title=" + 'test'+ "&veaction=edit")
# time.sleep(20)

def readAndUpload(rootDir):
    list_dirs = os.walk(rootDir)
    for root, dirs, files in list_dirs:
        for f in files:
            if 'doc' in os.path.join(root,f):
                try:
                    document = Document(str(os.path.join(root, f)))
                    tables = document.tables
                    title = tables[0].rows[0].cells[2].text
                    print(title)
                except Exception as e:
                    print(e)
                connection.get("http://***.***.***.***/index.php?title=" + title + "&veaction=edit")
                try:
                    word = win32com.client.Dispatch("Word.Application")
                    word.Application.Run("selectAndCopy", os.path.join(root, f))
                except Exception as e:
                    print('word处理出错!')

                time.sleep(5)
                time.sleep(20)
                #
                # try:
                #     textBox = connection.find_element_by_xpath('//*[@id="bodyContent"]/div[5]/div[1]')
                # except Exception:
                #     print('获取输入框爆炸')
                # textBox.click()
                # textBox=connection.find_element_by_xpath('//*[@id="searchInput"]')

                try:
                    connection.find_element_by_xpath(
                        '//*[@id="content"]/div/div[1]/div/div[1]/div[1]/div/div[1]/div[1]/div/span[2]/div/div[1]/div[1]/div/a/span[1]').click()
                except Exception:
                    print('关闭弹框爆炸')
                shell = win32com.client.Dispatch("WScript.Shell")
                shell.AppActivate("Chrome")
                shell.SendKeys("^v", 0)
                time.sleep(2)
                connection.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div[1]/div[2]/a').click()
                time.sleep(2)
                connection.find_element_by_xpath('cml/body/div[7]/div/div/div[1]/div[2]/div[2]/div/div[1]/div[2]/textarea').send_keys('ROBOT EDITED')
                connection.find_element_by_xpath('cml/body/div[7]/div/div/div[1]/div[2]/div[1]/div/div[1]/div/a').click()
                time.sleep(3)
                shell = win32com.client.Dispatch("WScript.Shell")
                shell.AppActivate("Chrome")
                shell.SendKeys("~", 0)
                time.sleep(5)
                # connection.find_element_by_xpath('cml/body/div[7]/div/div/div[1]/div[2]/div[1]/div/div[1]/div/a').click()
                # time.sleep(1)
                # except Exception as e:
                #     print(os.path.join(root,f)+u"不能被处理,文档错误")
                #     print(e)

readAndUpload(unicode("C:\Users\********\Desktop\upload",'utf-8'))

至于为什么有那么多time.sleep,而且都这么久,原因很简单,公司的网1Mbps,打开这个网页一分钟,,调试一次5分钟起步,心态爆炸

Contents