Spaces:
Running
Running
Commit ·
ca12da8
1
Parent(s): 76ae335
fix: use BeautifulSoup for OG image extraction (handles name=, twitter:image, article:image, link[rel=image_src])
Browse files- src/extractor.py +6 -17
src/extractor.py
CHANGED
|
@@ -38,7 +38,8 @@ class ArticleExtractor:
|
|
| 38 |
return None
|
| 39 |
text = result['text']
|
| 40 |
title = result.get('title') or self._extract_title_meta(downloaded) or ""
|
| 41 |
-
|
|
|
|
| 42 |
published, published_iso = self._parse_trafilatura_date(result.get('date'))
|
| 43 |
return Article(
|
| 44 |
url=url, title=title, text=text, source_domain=domain,
|
|
@@ -97,21 +98,6 @@ class ArticleExtractor:
|
|
| 97 |
m = re.search(r'<title[^>]*>(.*?)</title>', html, re.IGNORECASE | re.DOTALL)
|
| 98 |
return html_mod.unescape(m.group(1).strip()) if m else None
|
| 99 |
|
| 100 |
-
@staticmethod
|
| 101 |
-
def _extract_og_image(html: str) -> str:
|
| 102 |
-
import re
|
| 103 |
-
m = re.search(
|
| 104 |
-
r'<meta\s+[^>]*property=["\']og:image["\'][^>]*content=["\']([^"\']+)["\']',
|
| 105 |
-
html, re.IGNORECASE,
|
| 106 |
-
)
|
| 107 |
-
if m:
|
| 108 |
-
return m.group(1)
|
| 109 |
-
m = re.search(
|
| 110 |
-
r'<meta\s+[^>]*content=["\']([^"\']+)["\'][^>]*property=["\']og:image["\']',
|
| 111 |
-
html, re.IGNORECASE,
|
| 112 |
-
)
|
| 113 |
-
return m.group(1) if m else ""
|
| 114 |
-
|
| 115 |
@staticmethod
|
| 116 |
def _extract_fallback_date(soup) -> tuple[str, str]:
|
| 117 |
from datetime import datetime
|
|
@@ -155,10 +141,13 @@ class ArticleExtractor:
|
|
| 155 |
|
| 156 |
@staticmethod
|
| 157 |
def _extract_og_image_soup(soup) -> str:
|
| 158 |
-
for prop in ("og:image", "twitter:image"):
|
| 159 |
tag = soup.find("meta", property=prop) or soup.find("meta", attrs={"name": prop})
|
| 160 |
if tag and tag.get("content"):
|
| 161 |
return tag["content"]
|
|
|
|
|
|
|
|
|
|
| 162 |
return ""
|
| 163 |
|
| 164 |
|
|
|
|
| 38 |
return None
|
| 39 |
text = result['text']
|
| 40 |
title = result.get('title') or self._extract_title_meta(downloaded) or ""
|
| 41 |
+
soup = BeautifulSoup(downloaded, "html.parser")
|
| 42 |
+
image = self._extract_og_image_soup(soup)
|
| 43 |
published, published_iso = self._parse_trafilatura_date(result.get('date'))
|
| 44 |
return Article(
|
| 45 |
url=url, title=title, text=text, source_domain=domain,
|
|
|
|
| 98 |
m = re.search(r'<title[^>]*>(.*?)</title>', html, re.IGNORECASE | re.DOTALL)
|
| 99 |
return html_mod.unescape(m.group(1).strip()) if m else None
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
@staticmethod
|
| 102 |
def _extract_fallback_date(soup) -> tuple[str, str]:
|
| 103 |
from datetime import datetime
|
|
|
|
| 141 |
|
| 142 |
@staticmethod
|
| 143 |
def _extract_og_image_soup(soup) -> str:
|
| 144 |
+
for prop in ("og:image", "twitter:image", "article:image"):
|
| 145 |
tag = soup.find("meta", property=prop) or soup.find("meta", attrs={"name": prop})
|
| 146 |
if tag and tag.get("content"):
|
| 147 |
return tag["content"]
|
| 148 |
+
link = soup.find("link", rel="image_src")
|
| 149 |
+
if link and link.get("href"):
|
| 150 |
+
return link["href"]
|
| 151 |
return ""
|
| 152 |
|
| 153 |
|