-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
40 lines (39 loc) · 1.42 KB
/
Copy pathparser.py
File metadata and controls
40 lines (39 loc) · 1.42 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
from bs4 import BeautifulSoup
from backend.scraper.utils import clean_text
def parse_sections(html: str):
soup = BeautifulSoup(html, "html.parser")
sections = []
page_title = soup.title.string.strip() if soup.title else "Untitled Page"
meta_desc = ""
meta = soup.find("meta", attrs={"name":"description"})
if meta:
meta_desc = meta.get("content","")
meta_content=[page_title]
if meta_desc: meta_content.append(meta_desc)
sections.append({
"title":"Page Metadata",
"content":meta_content,
"word_count":sum(len(c.split()) for c in meta_content)
})
current_title="Introduction"
current_content=[]
for tag in soup.find_all(["h1","h2","h3","p","li"]):
if tag.name in ["h1","h2","h3"]:
if current_content:
sections.append({
"title":clean_text(current_title),
"content":current_content,
"word_count":sum(len(c.split()) for c in current_content)
})
current_title=tag.get_text()
current_content=[]
else:
text=clean_text(tag.get_text())
if text: current_content.append(text)
if current_content:
sections.append({
"title":clean_text(current_title),
"content":current_content,
"word_count":sum(len(c.split()) for c in current_content)
})
return sections