zoukankan      html  css  js  c++  java
  • 一键评教

    西安交通大学 校园网一键评教

    2017年12月


    __author__="zealousness"

    import urllib
    import ssl
    import http.cookiejar
    import re
    import requests
    import base64
    import json

    #关闭https的证书验证机制(否则你模拟的请求中还要附带证书)
    ssl._create_default_https_context=ssl._create_unverified_context

    #构造请求头
    header = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
    }

    # 使用opener的好处:每次请求cookie是不变的
    #构造一个opener来替代urlopen使用,以记住服务器给的cookie
    cookie=http.cookiejar.CookieJar()
    handler=urllib.request.HTTPCookieProcessor(cookie)
    opener=urllib.request.build_opener(handler)

    # 1、获取隐藏在html form表单里的隐藏元素lt和execution,以填入后面的登陆请求
    # 请求url:https://cas.xjtu.edu.cn/login
    req=urllib.request.Request("https://cas.xjtu.edu.cn/login")
    req.headers=header

    response_html=opener.open(req)#没加.read()


    # 2、将lt 和 execution从html表单的隐藏元素标签中抽取出来,将COOKIE从response中提取出来
    #提取lt和execution(表单中需要提交的元素,网站设计者一般会把它设计成隐藏元素藏在html代码中)
    text=response_html.read().decode(encoding='UTF8')
    lt=re.findall(r'name="lt"s*value="(.+)"',text)[0]
    execution=re.findall(r'name="execution"s*value="(.+)"',text)[0]

    #提取COOKIE
    text2=response_html.info()
    COOKIE=re.findall(r"JSESSIONID=(.+);s*Path",str(text2))[0]


    # 3、获取验证码(验证码图的url通过在图片上右键,检查,获取到相对url后猜测试探出实际url)
    # 请求url:https://cas.xjtu.edu.cn/ImageCodeServlet
    # req=urllib.request.Request("https://cas.xjtu.edu.cn/ImageCodeServlet?d=1510304595006")
    req=urllib.request.Request("https://cas.xjtu.edu.cn/ImageCodeServlet")
    req.headers=header
    #执行请求并获取图片
    response_img=opener.open(req)
    with open('code_xjtu.png','wb') as fn:
    fn.write(response_img.read())




    # 4、验证码识别
    # 百度云API
    try:
    temp_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=kabbLycgQC23YZwKvGY5AR9B&client_secret=iKvTYKc2skb7eRwXgV3SHFFCrz5aO2Hh'
    temp_res = requests.post(temp_url)
    temp_token = eval(temp_res.text)['access_token']
    temp_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' + temp_token
    temp_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    temp_file = open('code_xjtu.png', 'rb')
    temp_image = temp_file.read()
    temp_file.close()
    temp_data = {
    'image': base64.b64encode(temp_image)
    }
    temp_data = urllib.parse.urlencode(temp_data)
    temp_res = requests.post(url=temp_url, data=temp_data, headers=temp_headers)
    code = int(eval(temp_res.text)['words_result'][0]['words'])
    except Exception as e:
    print(e)




    # 5、登陆
    # 请求url:https://cas.xjtu.edu.cn/login
    # 如何找出请求的url?
    # 用浏览器查看登陆发送的request包,看requestURL部分,一般去掉requestURL后面乱七八糟的参数(可能有也可能没有)即可
    req=urllib.request.Request("https://cas.xjtu.edu.cn/login")
    #注意,虽然在浏览器F12中查看是看到url是这样的
    # "https://cas.xjtu.edu.cn/login;jsessionid=C1806BEB6C4BE03F71265D2F6A726B52"
    # 但实际上请求中的url只需要写"https://cas.xjtu.edu.cn/login"就可以了
    req.headers=header
    username=input("请输入netid")
    password=input("请输入密码")

    try:
    data = {
    "username": username,
    "password": password,
    "code": code,
    "lt": lt,
    "execution": execution,
    "_eventId": "submit",
    "submit": "登录"
    }
    except Exception as e:
    print(e)
    print("验证码识别失败了,再试一次吧!")


    data=urllib.parse.urlencode(data)
    data=data.encode('UTF8')
    response_result=opener.open(req,data=data)



    # 8、发送获取评教信息的请求
    req=urllib.request.Request("http://zhpj.xjtu.edu.cn/app/sshd4Stu/list.do?key=value")
    req.headers=header
    response_pj_list=opener.open(req)
    # 打印出response一看,是一个json字符串,则用json.loads()解析
    coursestr=response_pj_list.read().decode(encoding='UTF8')
    result=coursestr.split("},{")
    result[0]=result[0][2:]
    result[len(result)-1]=result[len(result)-1][:-2]

    for i in range(0,len(result)):
    result[i]="{"+result[i]+"}"

    try:
    coursejson = []
    for elem in result:
    print(elem)
    coursejson.append(json.loads(elem))
    except Exception as e:
    print(e)
    print("密码错误/教务处返回json格式异常,再试一次吧!")

    for elem in coursejson:
    req = urllib.request.Request("http://zhpj.xjtu.edu.cn/app/student/saveForm.do")
    req.headers = header
    data = {
    "standard_id": "591bb2f9c644dc05b08c82c4",
    "standard_name": "理论课评价指标",
    "jxbid": elem['jxbid'],
    "jsbh": elem['jsbh'],
    "kcdm": elem['kcdm'],
    "591c08fec644dc05b08c8ab3": 100,
    "591bb91bc644dc05b08c82fb": 100,
    "591bbb10c644dc05b08c8386": 100,
    "591bbc9dc644dc05b08c8416": 80,
    "591bbd27c644dc05b08c8425": 100,
    "591bbd55c644dc05b08c8426": 100,
    "591bbda5c644dc05b08c8427": 100,
    "591bbe2bc644dc05b08c842b": 100,
    "591bbe5dc644dc05b08c842c": 100,
    "591bbf46c644dc05b08c8430": 100,
    "591bbfa2c644dc05b08c8431": 100,
    "591bc1fec644dc05b08c850f": "好",
    "591bc547c644dc05b08c859f": 100,
    "591bc774c644dc05b08c8653": 100,
    "591bca1cc644dc05b08c8690": 100,
    "591bcc1dc644dc05b08c8691": 80
    }
    data = urllib.parse.urlencode(data)
    data = data.encode('UTF8')
    response_yjpj = opener.open(req, data=data)
    output=response_yjpj.read().decode(encoding='UTF8')
    if "true" in output:
    print("课程 ",elem["kcmc"]," 评价成功!")
  • 相关阅读:
    call、apply、bind函数的理解以及手写。
    父div里两个子div(inline-block),为什么两个子div中间会有小缝隙,如何解决?
    手写柯里化
    arguments的理解
    New
    BFC
    useCallBack和useMemo的用法
    观察者模式和发布订阅模式
    grid布局
    Android常见输入法的包名和主类名
  • 原文地址:https://www.cnblogs.com/zealousness/p/8040698.html
Copyright © 2011-2022 走看看