python知网自动抢面食小工具

wylc123 1年前 ⋅ 3285 阅读

自己动手丰衣足食 :)

#!usr/bin/env python
# -*- coding: utf-8 -*-
#!文件类型: python
#!创建时间: 2019/1/23 15:03
#!作者: SongBin
#!文件名称: GetNoodle_Old.py
#!简介:知网自动抢面食(新版)

import time
import win32api

import win32con
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, ElementNotInteractableException, ElementNotVisibleException, TimeoutException
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import logging
logging.basicConfig(level=logging.INFO,#控制台打印的日志级别
                    filename='C:\softs\kgexe\output.log',
                    filemode='a',##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
                    #a是追加模式,默认如果不写的话,就是追加模式
                    format=
                    '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                    #日志格式
                    )
dcap = dict(DesiredCapabilities.PHANTOMJS)  # 设置userAgent
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 ")
chrome_options = Options()
chrome_options.add_argument("--headless")
count = 1
while count > 0:
    try:
        # 打开浏览器
        logging.error("程序开始运行###########")
        print("程序开始运行###########")
        # browser = webdriver.PhantomJS(executable_path='C:\\softs\\phantomjs-2.0.0-windows\\bin\\phantomjs')  # .Firefox()  # .PhantomJS(desired_capabilities=dcap) # executable_path='/usr/local/bin/phantomjs'  phantomjs没有设置环境变量时可加参数
        browser = webdriver.Chrome(executable_path='C:\\softs\\chrome\\chromedriver.exe',options=chrome_options)  # .Firefox()  # .PhantomJS(desired_capabilities=dcap) # executable_path='/usr/local/bin/phantomjs'  phantomjs没有设置环境变量时可加参数
        loginurl = 'http://oa.cnki.net/web/TTKN/Default.aspx'
        # browser.maximize_window()
        browser.get(loginurl)
        WebDriverWait(browser, 5, 1).until(EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/div')))
        browser.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/input').send_keys('') #账号
        browser.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/input').send_keys("") #密码
        browser.find_element_by_id('login').click()
        time.sleep(10)

        mainwin = browser.current_window_handle #主窗口
        # WebDriverWait(browser, 5, 1).until(EC.presence_of_element_located((By.ID, 'iframe_home')))
        time.sleep(10)
        # 切换到当前frame
        browser.switch_to.frame("iframe_home")
        time.sleep(10)
        try:
            nodingcan = '订餐消息'
            try:
                nodingcan = browser.find_element_by_css_selector('.WFToDo_Table_left > div:nth-child(2) > p:nth-child(2)').text
            except:
                pass
            if nodingcan=='暂无待办事项':
                logging.info("没有订餐消息!")
                print("没有订餐消息!")
                browser.quit()
                #time.sleep(60)  # 推迟执行的秒数。
            else:
                dingcanlis = browser.find_elements_by_css_selector('ul.WFToDo_Table_left_ul.WFToDo_Table_left_ul_todo>li.WFToDo_Table_left_li')
                dca = None
                for dingcanli in dingcanlis:
                    try:
                        dca = dingcanli.find_element_by_xpath('p[contains(text(),"公司订餐预定提醒")]')
                        break
                    except:
                        pass
                if dca==None:
                    logging.info("没有订餐消息!")
                    print("没有订餐消息!")
                    browser.quit()
                    # time.sleep(60)  # 推迟执行的秒数。
                    pass
                else:
                    dca.click()
                    # 双线操作
                    driver = webdriver.Chrome(executable_path='C:\\softs\\chrome\\chromedriver.exe')
                    loginurl2 = 'http://oa.cnki.net/web/TTKN/Default.aspx'
                    driver.get(loginurl2)
                    WebDriverWait(browser, 5, 1).until(
                        EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/div')))
                    driver.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/input').send_keys('') #账号
                    driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/input').send_keys("") #密码
                    driver.find_element_by_id('login').click()
                    # 双线操作
                    time.sleep(5)
                    browser.find_element_by_css_selector('button.WFToDo_target_lookdetail:nth-child(2)').click()
                    time.sleep(10)
                    # 切换到订餐列表窗口
                    all_window = browser.window_handles
                    sreach_window = browser.current_window_handle
                    for winHandle in all_window:
                        if winHandle == mainwin:
                            pass
                        else:
                            sreach_window = winHandle
                            browser.switch_to.window(sreach_window)
                    time.sleep(10)
                    browser.find_element_by_xpath('/html/body/form/div[5]/table/tbody/tr[1]/td/div/div[1]/div[2]/b/a').click()
                    time.sleep(10)
                    # 切换到订餐编辑窗口
                    all_window = browser.window_handles
                    dingcanedit_window = browser.current_window_handle
                    for winHandle in all_window:
                        if winHandle == sreach_window or winHandle == mainwin:
                            pass
                        else:
                            dingcanedit_window = winHandle
                            browser.switch_to.window(dingcanedit_window)
                    time.sleep(10)
                    # browser.get_screenshot_as_file("D:\\baidu_error.jpg")
                    logging.error("运行脚本")
                    print("运行脚本")
					#早餐预定
                    browser.execute_script('for(i=1;i<10;i++){$("#repeaterData_ctl0"+i+"_RBZC_Type_2").prop("checked",true);} $("#repeaterData_ctl10_RBZC_Type_2").prop("checked",true);')
                    # 午餐预定
                    browser.execute_script('for(i=1;i<10;i++){$("#repeaterData_ctl0"+i+"_RB_Type_1").prop("checked",true);} $("#repeaterData_ctl10_RB_Type_1").prop("checked",true);')
                    browser.find_element_by_xpath('//*[@id="BTAdd1"]').click()
                    logging.error("自动订餐成功,请查看!")
                    print("自动订餐成功,请查看!")
                    # 成功后显示
                    successUrl = browser.current_url
                    browser.quit()
                    browser1 = webdriver.Chrome(executable_path='C:\\softs\\chrome\\chromedriver.exe')
                    loginurl1 = 'http://oa.cnki.net/web/TTKN/Default.aspx'
                    browser1.get(loginurl1)
                    WebDriverWait(browser, 5, 1).until(
                        EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/div')))
                    browser1.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/input').send_keys('') #账号
                    browser1.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/input').send_keys("") #密码
                    browser1.find_element_by_id('login').click()
                    time.sleep(10)
                    browser1.set_window_size(1024, 500)  # 设置窗口大小
                    browser1.get(successUrl)
                    # 成功后显示
                    count = 0
                    break
                    #win32api.MessageBox(0, "自动订餐成功,请查看!", "消息框标题", win32con.MB_OK)
        except (NoSuchElementException,ElementNotInteractableException,ElementNotVisibleException,TimeoutException):
            logging.info("没有订餐消息!")
            print("没有订餐消息!")
            browser.quit()
            #time.sleep(60)#推迟执行的秒数。
            pass
    except Exception as e:
        logging.error("异常信息:"+str(e))
        print("异常信息:"+str(e))
        browser.quit()
        #win32api.MessageBox(0, "出现异常!", "消息框标题", win32con.MB_OK)
        count = 0
        break

可部署成服务,后台自动监控抢面食。

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: