Athony

选课尝试
title: 学校python选课尝试 date: 2018-11-21 21:10:20 import requ...
扫描右侧二维码阅读全文
18
2019/06

选课尝试

title: 学校python选课尝试
date: 2018-11-21 21:10:20

import requests
import urllib
from PIL import Image
from lxml import etree
def get_viewstate(response):
    # 用Lxml库解析网页,通过Xpath语法定位__VIEWSTATE
    selector = etree.HTML(response.content)
    __VIEWSTATE = selector.xpath('//*[@id="form1"]/input/@value')[0]
    return  __VIEWSTATE

def login_status(response):
    if response.status_code == requests.codes.ok:
        selector = etree.HTML(response.text)
        state = selector.xpath('/html/head/title/text()')[0]
        if state == "正方教务管理系统":
            print("登录成功")
            return True
        elif state == "欢迎使用正方教务管理系统!请登录":
            print("登录失败")
            return False
    else:
        print(response.status_code)
        print("登录失败")
        return False
def login_shouye():
    url = "http://xk.zucc.edu.cn/default2.aspx"
    codeurl="http://xk.zucc.edu.cn/CheckCode.aspx?"
    reponseurl="http://xk.zucc.edu.cn/xs_main.aspx?xh=31702283"

    sess = requests.session()
    data={
        '__VIEWSTATE':'',
        'txtUserName': '31702283',
        'Textbox1':'', 
        'TextBox2': 'wzjly1314',
        'RadioButtonList1': '学生'.encode('gb2312'),#说明为学生身份
        'Button1': '',
        'lbLanguage': '',
        'hidPdrs': '',
        'hidsc': ''
    }

    headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
        'Referer': url,
        #'Cookie':'AntiLeech=2671741908; ASP.NET_SessionId=idIc9sa-zoS19kz5-uztOef66inN0cD1WmDgTbjaSYQpHcCjJF4*',

    }

    valcode = sess.get(codeurl,stream=True)
    f = open('valcode.jpg','wb')
    f.write(valcode.content)
    f.close()

    res1  = sess.get(url,headers=headers)
    viewstate = str(get_viewstate(res1))
    data['__VIEWSTATE'] = str(viewstate)

    img = Image.open('valcode.jpg')
    img.show()
    data['txtSecretCode']=str(input())

    res =  sess.post(url,data=data,headers=headers)
    login_status(res)
    #print(res)

login_shouye()
mpkj
登录成功
from  bs4 import BeautifulSoup
import re
class xuanke:
    def __init__(self, xh, passwd,sdname):
        self.xh=xh;
        self.passwd=passwd
        self.view = 0
        self.sdname = sdname
        self.real_base_url = 'http://xk.zucc.edu.cn/'
        self.xkurl = 'http://xk.zucc.edu.cn/xf_xsqxxxk.aspx?xh=31702283&xm=%CD%F5%D5%C1&gnmkdm=N121102'
        #公选课所用到的信息:
        self.base_data={
            '__EVENTTARGET':'', 
            '__EVENTARGUMENT': '',
            '__VIEWSTATE':'', #每次请求变化
            'ddl_kcxz': '', 
            'ddl_ywyl':'' ,
            'ddl_kcgs': '',
            'ddl_xqbs': '1',#据说是校园数据
            'ddl_sksj': '',
            'TextBox1': '',#课程搜索
            'dpkcmcGrid:txtChoosePage': '1',  
            'dpkcmcGrid:txtPageSize': '200',
            'dpkcmcGrid:btnNextPage': ''
        }
        self.headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
            'Referer': '',
            #'Cookie':'AntiLeech=2671741908; ASP.NET_SessionId=idIc9sa-zoS19kz5-uztOef66inN0cD1WmDgTbjaSYQpHcCjJF4*',

    }
    def get_self_viewstate(self,soup):
        _viewstate = soup.find('input',attrs={'name': '__VIEWSTATE'})
        self.base_data['__VIEWSTATE'] = _viewstate['value']

    def  get_login (self):
        request = self.xkurl
        dada1 = {
            'xh':self.xh,
            'xm': self.sdname.encode('gb2312'),
            'gnmkdm': 'N121102'
        }
        self.headers[ 'Referer']=self.real_base_url+'xs_main.aspx?xh='+self.xh
        request = requests.get(self.real_base_url+'xf_xsqxxxk.aspx',params=data1,headers=self.headers)

        self.headers[ 'Referer']=request.url
        soup = BeautifulSoup(request.content,'lxml')
        print(request.text)
       # self.headers[ '__VIEWSTATE']=self.get_self_viewstate(soup)

    def  __find_lessons(self,lesson_name=""):
        self.base_data['TextBox1']=lesson_name.encode('gb2312')
        url = 'http://xk.zucc.edu.cn/xs_main.aspx?xh=31702283'
        request = requests.post(url,data=self.base_data,headers=self.headers)
        soup = BeautifulSoup(request.text,'lxml')
        self.get_self_viewstate(soup)
        return self._getlessons(soup)

    class lesson:
        def __init__(self,name,code,teacher_name,Time,number):
            self.name = name
            self.code = code
            self.teacher_name =teacher_name
            self.time = Time
            self.number = number
        def show(self):
            print("name:"+self.name+"code:"+self.code+"teacher_name:"+self.teacher_name+"time:"+self.time+"number:"+self.number)

    def _get_lessons(self,soup):
        lesson_list=[]
        lessons_tag = soup.find('table',id='kcmcGrid')
        lesson_tag_list = lessons_tag.find_all('tr')[1:]
        for lesson_tag in lesson_tag_list:
            td_list = lesson_tag.find__all('td')
            coursecode =  td.list[0].input['name']
            name = td.list[1].string
            teacher_name = td.list[3].string
            time = td.list[4]['title']
            number = td.list[10].string
            lesson = self.lesson(name,coursecode,teacher_name,time,number)
            leeson_list.append(lesson)
        return lesson_list

    #选课
    def _select_lesson(self,lesson_list):
        data = copy.deepcopy(self.base_data) #深度复制
        data['Button1'] = ' 提交 '.encode('gb2312')
        for lesson in lesson_list:
            code = lesson.code
            data[code] ='on'
            url = 'http://xk.zucc.edu.cn/xs_main.aspx?xh=31702283'
            request = requests.post(url,data=data,headers=self.headers)
            soup = BeautifulSoup(request.text,'lxml')
            self.get_self_viewstate(soup)
            erroe_tag = soup.html.head.script
            if not error_tag is None:
                error_tag_text = error.tag.string
                r  = "arert\('(.+?)'\);"
                for s in re.findall(r,error_tag_text):
                    print(s)
            print("已选课程:")
            selected_lessons_pre_tag = soup.find('lengend',text="已选课程")
            selected_lessons_tag = selected_pre_tag.next_sibling
            tr_list = selected_lessons_tag.find_all("tr")[1:]
            for tr in tr_list:
                td = tr.find("td")
                print(td.string)
#xuanke1 = xuanke('xh','passwd','studentname')
xuanke1 = xuanke('31702283','wzjly1314','王樟')
xuanke1.get_login()
最后修改:2019 年 07 月 28 日 05 : 04 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论

2 条评论

  1. athony

    滴!访客卡!请上车的乘客系好安全带,现在是:Fri Jul 26 2019 19:04:14 GMT+0800 (中国标准时间)

  2. wz

    emm

© 2018-2019 Copyright   | 浙ICP备18047860号-1| SiteMap