Spaces:
Running
Running
Update parser/extract_tables_by_article.py
Browse files
parser/extract_tables_by_article.py
CHANGED
|
@@ -9,7 +9,7 @@ from parser.table_extractorgo import tables_from_soup, table_to_struct
|
|
| 9 |
async def extract_tables_from_url(url: str, timeout: int = 10) -> Dict[str, Any]:
|
| 10 |
"""
|
| 11 |
جلب صفحة HTML من URL، استخراج الجداول المرتبطة بالمواد،
|
| 12 |
-
وإرجاعها كـ JSON بعد تجاهل الجداول الفارغة أو غير
|
| 13 |
"""
|
| 14 |
result: List[Dict[str, Any]] = []
|
| 15 |
|
|
@@ -20,20 +20,26 @@ async def extract_tables_from_url(url: str, timeout: int = 10) -> Dict[str, Any]
|
|
| 20 |
|
| 21 |
# تحليل HTML
|
| 22 |
soup = BeautifulSoup(html_content, "html.parser")
|
| 23 |
-
|
| 24 |
-
# استخراج جميع الجداول
|
| 25 |
tables = tables_from_soup(soup)
|
| 26 |
|
| 27 |
for table in tables:
|
| 28 |
struct = table_to_struct(table)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
if not struct["headers"]:
|
| 32 |
continue
|
|
|
|
| 33 |
if not struct["rows"]:
|
| 34 |
continue
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
continue
|
|
|
|
| 37 |
|
| 38 |
# البحث عن المادة الأقرب قبل الجدول
|
| 39 |
prev = table.find_previous(string=True)
|
|
|
|
| 9 |
async def extract_tables_from_url(url: str, timeout: int = 10) -> Dict[str, Any]:
|
| 10 |
"""
|
| 11 |
جلب صفحة HTML من URL، استخراج الجداول المرتبطة بالمواد،
|
| 12 |
+
وإرجاعها كـ JSON بعد تجاهل الجداول الفارغة أو غير القانونية.
|
| 13 |
"""
|
| 14 |
result: List[Dict[str, Any]] = []
|
| 15 |
|
|
|
|
| 20 |
|
| 21 |
# تحليل HTML
|
| 22 |
soup = BeautifulSoup(html_content, "html.parser")
|
|
|
|
|
|
|
| 23 |
tables = tables_from_soup(soup)
|
| 24 |
|
| 25 |
for table in tables:
|
| 26 |
struct = table_to_struct(table)
|
| 27 |
|
| 28 |
+
# ===== فلترة الجداول الفارغة أو غير المهمة =====
|
| 29 |
+
if not struct["headers"] or all(h.strip() == "" for h in struct["headers"]):
|
| 30 |
continue
|
| 31 |
+
|
| 32 |
if not struct["rows"]:
|
| 33 |
continue
|
| 34 |
+
|
| 35 |
+
all_rows_empty = True
|
| 36 |
+
for row in struct["rows"]:
|
| 37 |
+
if any(cell and len(cell.strip()) > 5 for cell in row):
|
| 38 |
+
all_rows_empty = False
|
| 39 |
+
break
|
| 40 |
+
if all_rows_empty:
|
| 41 |
continue
|
| 42 |
+
# ==============================================
|
| 43 |
|
| 44 |
# البحث عن المادة الأقرب قبل الجدول
|
| 45 |
prev = table.find_previous(string=True)
|