-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_to_json_converter.py
More file actions
82 lines (69 loc) · 2.8 KB
/
Copy pathhtml_to_json_converter.py
File metadata and controls
82 lines (69 loc) · 2.8 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import json
import sys
from bs4 import BeautifulSoup
def html_to_json(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
# 1. Parse tables if they exist
tables_data = []
for table_idx, table in enumerate(soup.find_all('table')):
headers = [th.get_text(strip=True) for th in table.find_all('th')]
rows = []
for tr in table.find_all('tr'):
cells = tr.find_all('td')
if cells:
row_data = {}
for cell_idx, cell in enumerate(cells):
header = headers[cell_idx] if cell_idx < len(headers) else f"column_{cell_idx}"
row_data[header] = cell.get_text(strip=True)
rows.append(row_data)
tables_data.append({
"table_index": table_idx,
"headers": headers,
"rows": rows
})
# 2. Parse general hierarchical text elements (headings and paragraphs)
hierarchical_content = []
current_section = {"section": "Root", "content": []}
for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'li']):
text = element.get_text(strip=True)
if not text:
continue
if element.name.startswith('h'):
if current_section["content"] or current_section["section"] != "Root":
hierarchical_content.append(current_section)
current_section = {
"section": text,
"level": int(element.name[1]),
"content": []
}
else:
current_section["content"].append({
"type": element.name,
"text": text
})
if current_section["content"] or current_section["section"] != "Root":
hierarchical_content.append(current_section)
# 3. Combine result
result = {
"title": soup.title.string if soup.title else "Untitled Document",
"tables": tables_data,
"document_structure": hierarchical_content
}
return result
if __name__ == "__main__":
# Example usage:
# python html_to_json_converter.py input.html output.json
if len(sys.argv) < 3:
print("Usage: python html_to_json_converter.py <input_html_path> <output_json_path>")
sys.exit(1)
input_path = sys.argv[1]
output_path = sys.argv[2]
try:
with open(input_path, 'r', encoding='utf-8') as f:
html = f.read()
json_data = html_to_json(html)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(json_data, f, indent=2, ensure_ascii=False)
print(f"Successfully converted '{input_path}' to '{output_path}'!")
except Exception as e:
print(f"Error during conversion: {e}")