def get_view_state(self, url_suffix, **kwargs): """ 获取页面 view_state 值""" res = self.get(url_suffix, allow_redirects=False, **kwargs) res.raise_for_status() if res.status_code == 302: raise requests.TooManyRedirects return get_view_state_from_html(res.text)
def _get_payload(self, html): ''' 获取课表post 的参数 ''' view_state = get_view_state_from_html(html) payload = { '__VIEWSTATE': view_state, ['xnd', 'xn'][self.schedule_type]: self.schedule_year, ['xqd', 'xq'][self.schedule_type]: self.schedule_term } return payload
def _get_login_payload(self, login_url, **kwargs): ''' 获取登录页面的 请求参数''' try: kwargs['timeout'] = 3 res = self._get(login_url, **kwargs) except RequestException: # 首次请求可能出现 Connection aborted res = self._get(login_url, **kwargs) view_state = get_view_state_from_html(res.text) return {'__VIEWSTATE': view_state}
def _get_login_payload(self, login_url, **kwargs): ''' 获取登录页面的 请求参数''' try: kwargs['timeout'] = 3 res = self._get(login_url, **kwargs) except RequestException: # 首次请求可能出现 Connection aborted res = self._get(login_url, **kwargs) url_info = res.url.split(login_url)[0].split('/(') if len(url_info) == 2: self._update_url_token('/(' + url_info[1]) view_state = get_view_state_from_html(res.text) return {'__VIEWSTATE': view_state}
def _get_api_by_bm(self, class_name, **kwargs): ''' 部门教师 查询学生班级课表 共3个请求''' # steps 1: 获取课表 view_state try: res = self._get(self.schedule_url, **kwargs) if res.status_code != 200: raise RequestException view_state = get_view_state_from_html(res.text) except RequestException: raise ScheduleException(self.code, '获取课表请求参数失败') # steps 2: 选择课表 学年学期 if self.schedule_year and self.schedule_term: payload = { '__VIEWSTATE': view_state, 'xn': self.schedule_year, 'xq': self.schedule_term } try: res = self._post(self.schedule_url, data=payload, **kwargs) if res.status_code != 200: raise RequestException except RequestException: raise ScheduleException(self.code, '获取课表请求参数失败') # steps 3: 获取课表数据 payload = self._get_payload_by_bm(res.content.decode('gbk'), class_name) try: res = self._post(self.schedule_url, data=payload, **kwargs) if res.status_code != 200: raise RequestException except RequestException: raise ScheduleException(self.code, '获取课表信息失败') html = res.content.decode('gbk') schedule = ScheduleParse(html, self.time_list, self.schedule_type).get_schedule_dict() return schedule
def get_view_state(self, url_suffix, **kwargs): """ 获取页面 view_state 值""" res = self.get(url_suffix, **kwargs) return get_view_state_from_html(res.text)
def get_view_state(self, url_suffix, **kwargs): res = self.get(url_suffix, allow_redirects=False, **kwargs) if res.status_code != 200: raise requests.RequestException return get_view_state_from_html(res.text)