-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_data.py
99 lines (80 loc) · 3.07 KB
/
parsing_data.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import requests
from bs4 import BeautifulSoup, NavigableString, Tag
from urllib.parse import urljoin
from PIL import Image
from io import BytesIO
from concurrent.futures import ThreadPoolExecutor, as_completed
def get_page_content(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.text
except requests.RequestException as e:
print(f"Error fetching the URL: {e}")
return None
def is_valid_section(tag):
# Heuristic to identify a valid section
if tag.name in ["section", "article", "p"]:
if tag.get('class', None):
class_names = [cls.lower() for cls in tag.get('class')]
if 'footer' in class_names or 'header' in class_names or 'menu' in class_names:
return False
return True
return False
def get_text_from_section(tag):
texts = []
for elem in tag.descendants:
if isinstance(elem, NavigableString):
parent = elem.parent
if parent.name not in ['script', 'style'] and isinstance(parent, Tag):
texts.append(elem.strip())
return ' '.join(texts).strip()
def fetch_image_size(url):
try:
response = requests.get(url)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
return url, image.size[0] * image.size[1]
except Exception as e:
return url, 0
def get_images_from_section(tag, base_url):
image_urls = [urljoin(base_url, img['src'])
for img in tag.find_all('img') if img.get('src')]
largest_image_url = None
max_size = 0
with ThreadPoolExecutor() as executor:
future_to_url = {executor.submit(
fetch_image_size, url): url for url in image_urls}
for future in as_completed(future_to_url):
url, size = future.result()
if size > max_size:
max_size = size
largest_image_url = url
return [largest_image_url] if largest_image_url is not None else []
def chunk_webpage(files):
chunks = []
for file_row in files:
html_content = file_row['content']
url = file_row['url']
g_images = file_row['images']
soup = BeautifulSoup(html_content, 'html.parser')
body_content = soup.body if soup.body else soup
for section in body_content.find_all(is_valid_section):
text = get_text_from_section(section)
if g_images != None:
images = get_images_from_section(section, url)
images.extend(g_images)
else:
images = None
if text or images:
chunk = {'text': text, 'images': images, 'url': url}
chunks.append(chunk)
# Fallback strategy: Use the entire body content
if not chunks:
text = get_text_from_section(body_content)
images = get_images_from_section(body_content, url)
images.extend(g_images)
if text or images:
chunk = {'text': text, 'images': images, 'url': url}
chunks.append(chunk)
return chunks