-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (47 loc) · 1.77 KB
/
main.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
def extract_header_content(html_content: str) -> str:
"""
Extracts the content of the <head> tag from the given HTML content.
:param html_content: The full HTML content as a string.
:return: The extracted header content.
"""
start_index = html_content.find("<head>")
end_index = html_content.find("</head>")
if start_index != -1 and end_index != -1:
return html_content[start_index + len("<head>"):end_index].strip()
print("WARNING could not extract header contents")
return ""
def extract_body_content(html_content: str) -> str:
"""
Extracts the content of the <body> tag from the given HTML content.
:param html_content: The full HTML content as a string.
:return: The extracted body content.
"""
start_index = html_content.find("<body>")
end_index = html_content.find("</body>")
if start_index != -1 and end_index != -1:
return html_content[start_index + len("<body>"):end_index].strip()
print("WARNING could not extract body contents")
return ""
def add_text_to_header_and_body_of_html(html_content :str, head_text: str, body_text: str) -> str:
# find the closing head tag and insert the head_text before it
head_index = html_content.find("</head>")
if head_index != -1:
html_content = (
html_content[:head_index] + head_text + "\n" + html_content[head_index:]
)
# find the closing body tag and insert the body_text before it
body_index = html_content.find("</body>")
if body_index != -1:
html_content = (
html_content[:body_index] + body_text + "\n" + html_content[body_index:]
)
return html_content
BLANK_HTML_FILE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
"""