This repository was archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_utils.py
54 lines (40 loc) · 1.44 KB
/
parser_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import re
from json import JSONDecoder
from bs4 import BeautifulSoup
class NoQuestionFound(Exception):
pass
class ParseError(Exception):
pass
QNUM_REGEX = re.compile(r"var qnum = .+;")
FIND_DIGIT_REGEX = re.compile(r"\d+")
AAID_REGEX = re.compile(r"aaid=.+")
class Parser:
@staticmethod
def parse(page: str):
try:
current_question_script = str(Parser.find_tags(page)[-4])
_json = list(Parser.extract_json(current_question_script))[0] # extract json and select first object
qid = _json['id']
type_ = _json['answer']['type']
qnum = FIND_DIGIT_REGEX.findall(
QNUM_REGEX.findall(current_question_script)[0])[0] # extract question number
#return {'qid': qid, 'qnum': qnum}, type_
return {'qnum': qnum}, type_
except (KeyError, IndexError) as e:
raise NoQuestionFound(e)
@staticmethod
def find_tags(page: str):
return BeautifulSoup(page, 'html.parser').find_all('script')
@staticmethod
def extract_json(string: str):
pos = 0
while True:
match = string.find('{', pos)
if match == -1:
break
try:
result, index = JSONDecoder().raw_decode(string[match:])
yield result
pos = match + index
except ValueError:
pos = match + 1