-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_html.py
60 lines (58 loc) · 1.86 KB
/
parse_html.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
from bs4 import BeautifulSoup
import glob
import subprocess
import sqlite3
import datetime
import os
try:
os.remove("records.db")
except FileNotFoundError:
pass
con = sqlite3.connect("records.db")
cur = con.cursor()
# Create table
cur.execute(
"""CREATE TABLE entries
(record_id int, date text, who text, org text, event_type text, event_text text, event_other text)"""
)
record_id = 0
for fpath in glob.glob("html/*.html"):
with open(fpath, "r") as f:
html = f.read()
soup = BeautifulSoup(html, "html.parser")
for consultation in soup.find_all(id="patientRecordCon"):
record_id += 1
date, who, org = [x.text for x in consultation.find_all("td")]
# events = []
# 29 Oct 2020
date = datetime.datetime.strptime(date, "%d %b %Y").isoformat()
entry = consultation
while entry := entry.find_next_sibling():
if entry.get("id") == "patientRecordCon":
# breakpoint()
break
event_type, event_text, event_other = [
x.get_text("\n") for x in entry.find_all("td")
]
cur.execute(
"INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?, ?)",
(
record_id,
date,
who,
org,
event_type,
event_text,
event_other,
),
)
# events.append((event_type, event_text, event_other))
# print(date, name, org)
# print(events)
# print()
# breakpoint()
con.commit()
con.close()
subprocess.check_call(
["sqlite-utils", "enable-fts", "records.db", "entries", "event_text"]
)