Course-grabbing Code of Nanjing Agricultural University--python-based selenium package + Google browser

Posted by sleightofhand on Fri, 17 Dec 2021 23:03:47 +0100

Preface

I am an undergraduate in Nanjing Agricultural University. Because I can't grab classes, I wrote a code for grabbing classes, which is a little bug, but the effect is still good.

Small white studies, big man don't spray

Tip: The following is the main body of this article. The following cases can be used as reference.

1. Pre-preparation

1.python environment setup + VScode installation configuration

There are many tutorials on the Internet here, let alone say, recommend an UP master, nanny level tutorial

https://www.bilibili.com/video/BV17A411T73u?from=search&seid=2612128118445730416

2. Install selenium package + Google browser driver

Also recommend an up master with detailed tutorials

https://www.bilibili.com/video/BV1Z4411o7TA?p=1

3. Other methods

Of course, you can also install the python text compiler on the python website, using the pip directive to install the selenium package

2. Code

Code function is to query the course of Philosophical Methodology. If you find that the number of courses is not zero, Click to select the course. Otherwise, if you refresh the web page for 3s, the code will run until you find the corresponding course

Unable to lock elements during run, if run is interrupted, re-execute the code (this is a minor bug)

Note: All of the following codes are limited to the course selection of Philosophical Methods. This code is written in 2021/8/31. It is valid for personal testing. Later, it may be unable to be used due to website updates and other reasons. Later, it will not be maintained. It is limited to everyone learning and communicating. Please do not compete maliciously. Thank you!

from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
global driver
if __name__ == '__main__':
#The following line of code fills in the location of your Google browser driver
    driver_path=r'C:\Users\Wanmeng\AppData\Local\Google\Chrome\Application\chromedriver.exe'
    driver=webdriver.Chrome(executable_path=driver_path)
    driver.get("http://jw1.njau.edu.cn/jsxsd/xsxk/xklc_list")
    driver.implicitly_wait(5)
    time.sleep(2)
    studynum=driver.find_element_by_css_selector('#username')
 #####################################################################   
    studynum.send_keys('---------')#Fill in the student number of the website of the Southern Agricultural and Educational Administration here and remember to quote it
#######################################################################
    studypassword=driver.find_element_by_css_selector('#password')
#################################################################
    studypassword.send_keys('---------')#Fill in the login password of the website of the South Agricultural Education Department here
#################################################################
    login=driver.find_element_by_css_selector('#login_submit')
    login.click()
    chooselesson=driver.find_element_by_css_selector('[href="/jsxsd/ggly/ysgg_query?Ves632DSdyV=NEW_XSD_PYGL"]')
    chooselesson.click()
    choosecase=driver.find_element_by_css_selector('[href="/jsxsd/xsxk/xklc_list"]')
    choosecase.click()
    driver.find_element_by_xpath("/html/body/div[4]/div[2]/table/tbody/tr[2]/td[4]/a").click()
    #driver.switch_to_frame("/html/body/div[4]/div[2]/table/tbody/tr[2]/td[4]/a")
    driver.switch_to.window(driver.window_handles[-1])
    driver.find_element_by_css_selector('[href="/jsxsd/xsxkkc/comeInGgxxkxk"]').click()
    driver.switch_to.frame("mainFrame")
    Select(driver.find_element_by_css_selector('[name="szjylb"]')).select_by_value('14')
    #driver.find_element_by_xpath("/html/body/div[2]/select[1]").click()
    #time.sleep(3)
    # s1 = Select(driver.find_element_by_css_selector('[id="szjylb"][name="szjylb"]'))
    # s1.select_by_value("14")
    driver.find_element_by_xpath("/html/body/div[2]/input[6]").click()
    # txt=driver.find_element_by_xpath("/html/body/div[3]/div/table/tbody/tr[1]/td[9]")
    # print(txt.text)
    school0=driver.find_elements_by_css_selector('.odd > td:nth-child(8)')
    school1=driver.find_elements_by_css_selector('.even > td:nth-child(8)')
    amount0=driver.find_elements_by_css_selector('.odd > td:nth-child(9)')
    amount1=driver.find_elements_by_css_selector('.even > td:nth-child(9)')
    judge0=driver.find_elements_by_css_selector('.odd > td:nth-child(10)')
    judge1=driver.find_elements_by_css_selector('.even > td:nth-child(10)')
    enter0=driver.find_elements_by_css_selector('.odd > td:nth-child(12) a')
    enter1=driver.find_elements_by_css_selector('.even > td:nth-child(12) a')
    xuanke=0
    driver.implicitly_wait(5)
    while True:
        if xuanke==0:
            for i,j,k,h in zip(amount0,judge0,enter0,school0):
                if i.text != '0' and ('conflict' not in j.text) and h.text=='Pukou Campus' :
                    k.click()
                    alert = driver.switch_to_alert()
                    alert.accept()
                    xuanke=1
                    #break
            for l,m,n,g in zip(amount1,judge1,enter1,school1):
                if l.text != '0' and ('conflict' not in  m.text)  and g.text=='Pukou Campus':
                    n.click()
                    alert = driver.switch_to_alert()
                    alert.accept()
                    xuanke=1
            if xuanke==0:
                driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                driver.find_element_by_id('dataView_next').click()
                time.sleep(1.5)
                school0=driver.find_elements_by_css_selector('.odd > td:nth-child(8)')
                school1=driver.find_elements_by_css_selector('.even > td:nth-child(8)')
                amount0=driver.find_elements_by_css_selector('.odd > td:nth-child(9)')
                amount1=driver.find_elements_by_css_selector('.even > td:nth-child(9)')
                judge0=driver.find_elements_by_css_selector('.odd > td:nth-child(10)')
                judge1=driver.find_elements_by_css_selector('.even > td:nth-child(10)')
                enter0=driver.find_elements_by_css_selector('.odd > td:nth-child(12) a')
                enter1=driver.find_elements_by_css_selector('.even > td:nth-child(12) a')
                for i,j,k,h in zip(amount0,judge0,enter0,school0):
#The following code is for Pukou District only. If you are a Weigang School District, you need to change'Pukou School District'to'Weigang School District'
                    if i.text != '0' and ('conflict' not in j.text) and h.text=='Pukou Campus' :
                        k.click()
                        alert = driver.switch_to_alert()
                        alert.accept()
                        xuanke=1
                    #break
                for l,m,n,g in zip(amount1,judge1,enter1,school1):
                    if l.text != '0' and ('conflict'not in m.text) and g.text=='Pukou Campus':
                        n.click()
                        alert = driver.switch_to_alert()
                        alert.accept()
                        xuanke=1
            if xuanke==0:
                driver.refresh()
                time.sleep(2.2)
                driver.find_element_by_css_selector('[href="/jsxsd/xsxkkc/comeInGgxxkxk"]').click()
                driver.switch_to.frame("mainFrame")
                time.sleep(0.5)
                Select(driver.find_element_by_css_selector('[name="szjylb"]')).select_by_value('14')
                driver.find_element_by_xpath("/html/body/div[2]/input[6]").click()
                school0=driver.find_elements_by_css_selector('.odd > td:nth-child(8)')
                school1=driver.find_elements_by_css_selector('.even > td:nth-child(8)')
                amount0=driver.find_elements_by_css_selector('.odd > td:nth-child(9)')
                amount1=driver.find_elements_by_css_selector('.even > td:nth-child(9)')
                judge0=driver.find_elements_by_css_selector('.odd > td:nth-child(10)')
                judge1=driver.find_elements_by_css_selector('.even > td:nth-child(10)')
                enter0=driver.find_elements_by_css_selector('.odd > td:nth-child(12) a')
                enter1=driver.find_elements_by_css_selector('.even > td:nth-child(12) a')                
    time.sleep(3)
    

3. Operation results

 

Nannong rush classes

summary

Problematic little buddies can comment on it in the commentary area. The original is not easy. Give some support!

Catalog

Preface

1. Pre-preparation

1.python environment setup + VScode installation configuration

2. Install selenium package + Google browser driver

3. Other methods

2. Code

3. Operation results

 

summary

Topics: Python Pycharm Selenium