-
Notifications
You must be signed in to change notification settings - Fork 14
/
parser_utils.py
64 lines (52 loc) · 1.68 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
55
56
57
58
59
60
61
62
63
64
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:
"""
Handles parsing tasks
"""
@staticmethod
def parse(page: str):
"""
finds the javascript tags in question page then parses
JSON objects from javascript to extract the qid, qnum and type
"""
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_
except (KeyError, IndexError) as e:
raise NoQuestionFound(e)
@staticmethod
def find_tags(page: str):
"""
uses bs4 to parse tags
"""
return BeautifulSoup(page, 'html.parser').find_all('script')
@staticmethod
def extract_json(string: str):
"""
Extracts json objects from string
"""
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