-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_clar.py
42 lines (36 loc) · 1.35 KB
/
read_clar.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
import json
import re
import sys
CONTEST_URL = sys.argv[2]
SAVE_PATH = sys.argv[3]
def func(s):
m = re.search(r"<a href=\"(.*)\">(.*)</a>", s)
return "<%s%s|%s>" % (CONTEST_URL, m.group(1), m.group(2))
data = []
with open(sys.argv[1], encoding="utf-8") as f:
a = re.findall(r"<tbody>.*?</tbody>", f.read(), re.S)
if not a:
print("<tbody> not found")
else:
assert len(a) == 1
a = re.findall(r"<tr>.*?</tr>", a[0], re.S)
for x in a:
b = re.findall(r"<td.*?</td>", x, re.S)
title = func(b[0])
user_name = func(b[1])
assert re.fullmatch(r"<td>.*</td>", b[2], re.S)
assert re.fullmatch(r"<td>.*</td>", b[3], re.S)
question = re.sub(r"<.*?>", "", b[2][4:-5]).replace("'", "'")
response = re.sub(r"<.*?>", "", b[3][4:-5]).replace("'", "'")
public = re.sub(r"<.*?>", "", b[4])
update_url = CONTEST_URL + re.search(r"<a href=\"(.*?)\">", b[7]).group(1)
data.append({
"title": title,
"user_name": user_name,
"question": question,
"response": response,
"public": public,
"update_url": update_url,
})
with open(SAVE_PATH, "w", encoding="utf8") as f:
f.write(json.dumps(data, ensure_ascii=False))