2018年考研自动选/抢报考点

媳妇打算考研究生,报不了名,怎么办?

做为一个多年码农,这个忙还是要帮的。目的就是写个程序去「抢」报考点。

开发环境

使用python + selenium 搞定, 开发环境

仅仅花了2个小时就报名成功了!!!!

具体代码

#!/usr/bin/env python3.6

# -*- coding: utf-8 -*-
"""
使用说明:
http://blog.ndou.net/article/15/2018-postgraduate-examination-automatic-selection-rush-to-report-test-sites,
https://blog.csdn.net/enlangs/article/details/83026839

QQ群:931170975
"""

import logging
import os
import platform
import time

import selenium.webdriver.support.ui as ui
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

logging.basicConfig(
    level=logging.WARNING,
    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
)

t_system = platform.system()
if t_system == 'Windows':
    chromedriver_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver.exe')
    browser = webdriver.Chrome(chromedriver_path)
else:
    browser = webdriver.Chrome()

wait2s = ui.WebDriverWait(browser, 2)
wait10s = ui.WebDriverWait(browser, 10)
wait30s = ui.WebDriverWait(browser, 30)

url = "https://account.chsi.com.cn/passport/login?entrytype=yzgr&service=https%3a%2f%2fyz.chsi.com.cn%2fuser%2fcenter.jsp"


def click_select(name: str, opt_text: str):
    wait10s.until(
        EC.presence_of_element_located((By.XPATH, f"//select[@name='{name}']/option[contains(text(),'{opt_text}')]"))
    ).click()


def page_has_loaded(driver):
    """
    页面是否加载完
    :param driver:
    :return:
    """
    page_state = driver.execute_script('return document.readyState;')
    return page_state == 'complete'


try:
    # 登录
    browser.get(url)
    username = browser.find_element_by_id("username")
    username.send_keys("")  # 请再这儿输入身份证号码!
    password = browser.find_element_by_id("password")
    password.send_keys("")  # 请再这儿输入密码!
    password.send_keys(Keys.ENTER)

    # 点击网上报名
    wait10s.until(EC.title_is('用户中心_中国研究生招生信息网'))
    element = browser.find_element_by_xpath('//div[text()="网上报名"]')
    element.click()

    # 点击确定
    wait10s.until(EC.title_is('全国硕士研究生招生考试网上报名平台'))
    element = browser.find_element_by_xpath('//a[text()="确定"]')
    element.click()

    # 点击 填写报考信息
    wait10s.until(EC.visibility_of_element_located((By.CLASS_NAME, 'yzwb-box2-cnt')))
    element = browser.find_element_by_xpath('//div[text()="填写报考信息"]')
    element.click()

    # 点击 阅读完毕
    wait10s.until(lambda driver: driver.find_element_by_xpath('//button/span[text()="阅读完毕"]')).click()

    # 点击 同意 , 考生诚信考试承诺书界面, 要等10s才能点击。
    wait30s.until(
        EC.element_to_be_clickable((By.XPATH, '//button/span[text()="同意"]'))
    ).click()

    # 点击 确认 , 确认考生信息 界面
    wait10s.until(
        EC.element_to_be_clickable((By.XPATH, '//button/span[text()="确认"]'))
    ).click()

    # 报考单位 界面

    # 招生单位
    click_select('yzwb_zsdwss', '')  # 输入省份
    click_select('yzwb_bkdwdm', '')  # 输入学校

    # 考试方式
    click_select('yzwb_ksfsm', '')  # 输入考试方式
    # 专项计划
    click_select('yzwb_zxjh', '无')  # 输入专项计划, 默认无。
    # 报考类别
    click_select('yzwb_bklbm', '非定向就业')  # 输入报考类别, 默认:非定向就业

    wait10s.until(
        EC.element_to_be_clickable((By.XPATH, '//button/span[text()="下一步"]'))
    ).click()

    # 备用信息
    wait10s.until(
        EC.element_to_be_clickable((By.XPATH, '//button/span[text()="下一步"]'))
    ).click()

    # 报考专业

    # 报考院系所名称
    click_select('yzwb_mainyxsm', '')  # 报考院系所名称,默认无
    # 报考专业
    click_select('yzwb_mainzydm', '')  # 报考专业,默认无
    # 研究方向
    click_select('yzwb_mainyjfxm', '')  # 研究方向,默认无

    # 学习方式
    click_select('yzwb_mainbkxxfs', '')  # 学习方式,默认无
    # 考试科目
    click_select('yzwb_kskm', '')  # 考试科目,默认无

    wait10s.until(
        EC.element_to_be_clickable((By.XPATH, '//button/span[text()="下一步"]'))
    ).click()

    # 报考点

    # 报考点所在省(市)
    is_first = False
    while True:
        logging.warning('选择报考点进行提交!')
        if is_first:
            # 直接刷新,避免走下面2个select的ajax请求。
            browser.refresh()
        else:
            is_first = True
            click_select('yzwb_bmdss', '')  # 报考点所在省(市),默认无
            click_select('yzwb_bmddm', '')  # 报考点,默认无
            wait10s.until(
                EC.element_to_be_clickable((By.XPATH, '//button/span[text()="下一步"]'))
            ).click()

        while True:
            time.sleep(0.1)
            if page_has_loaded(browser):
                break

        logging.warning('检查是否成功提交报考点!')
        is_success = False
        # 查看是否有错误弹框div
        try:
            c = browser.find_element_by_xpath(
                xpath='//div[contains(@class, "ivu-notice-with-error") and contains(@class, "ivu-notice-with-desc")]'
            )
            error_msg = c.text
            if error_msg and '错误' in error_msg:
                time.sleep(2)
                continue
        except NoSuchElementException:
            try:
                wait2s.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(text(), "确认报名信息")]')))
                break
            except (NoSuchElementException, TimeoutException):
                time.sleep(2)
                continue

    # 确认报名信息
    wait10s.until(
        EC.element_to_be_clickable((By.XPATH, ".//*[contains(text(), '本人承诺信息完整属实,符合相关规定')]"))
    ).click()

    wait10s.until(
        EC.element_to_be_clickable((By.XPATH, '//button/span[text()="确认报名"]'))
    ).click()

    time.sleep(10000)
finally:
    browser.close()

使用说明

  1. 下载Python,见环境
  2. 解压或者是安装Python,在命令行下执行python --version, 如果显示Python 3.6.4就证明安装成功。
  3. 执行pip install selenium
  4. 把上面代码保存到本地,命名为s.py
  5. 运行python s.py运行python s.py 或者:C:\Python36\python.exe C:\Users\Administrator\Desktop\s.py
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值