Passed
Push — master ( e9e603...c36ccf )
by dai
04:57
created

school_api.client.api.utils   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A get_view_state_from_html() 0 12 3
A get_alert_tip() 0 6 2
1
# -*- coding: utf-8 -*-
2
from __future__ import absolute_import, unicode_literals
3
4
import re
5
6
from bs4 import BeautifulSoup
7
8
from school_api.exceptions import OtherException
9
10
11
def get_alert_tip(html):
12
    ''' 获取页面提示信息 '''
13
    tips = re.findall(r">alert\(\'(.*?)\'", html)
14
    if tips:
15
        return tips[0]
16
    return None
17
18
19
def get_view_state_from_html(html):
20
    ''' 获取 __VIEWSTATE 值 '''
21
    pre_soup = BeautifulSoup(html, "html.parser")
22
    view_state_soup = pre_soup.find(attrs={"name": "__VIEWSTATE"})
23
    try:
24
        view_state = view_state_soup['value']
25
    except TypeError:
26
        if html.find("网站防火墙") > -1:
27
            raise OtherException('', "请求被防火墙所拦截, 请降低请求频率")
28
        raise OtherException('', '获取view_state失败')
29
30
    return view_state
31