skander101 commited on
Commit
7ee4ebc
·
1 Parent(s): 623f05d

fix: restore image extraction in HTML scraper

Browse files
Files changed (1) hide show
  1. src/html_scraper.py +17 -3
src/html_scraper.py CHANGED
@@ -62,7 +62,8 @@ class HTMLSiteScraper:
62
  continue
63
  seen_urls.add(full_url)
64
  domain = site.get("domain") or urlparse(full_url).netloc
65
- published, published_iso = self._extract_date(soup, a_tag)
 
66
  post = RedditPost(
67
  id=full_url,
68
  title=title,
@@ -71,7 +72,7 @@ class HTMLSiteScraper:
71
  score=0,
72
  num_comments=0,
73
  source_domain=domain,
74
- image_url="",
75
  published=published,
76
  published_iso=published_iso,
77
  )
@@ -82,7 +83,7 @@ class HTMLSiteScraper:
82
  return posts
83
 
84
  @staticmethod
85
- def _extract_date(soup: BeautifulSoup, a_tag) -> tuple[str, str]:
86
  for parent_tag in ("div", "article", "li", "section"):
87
  parent = a_tag.find_parent(parent_tag)
88
  if not parent:
@@ -103,6 +104,19 @@ class HTMLSiteScraper:
103
  return parsed
104
  return ("", "")
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  @staticmethod
107
  def _find_article_links(soup: BeautifulSoup, site: dict) -> list:
108
  for selector in site["selectors"]:
 
62
  continue
63
  seen_urls.add(full_url)
64
  domain = site.get("domain") or urlparse(full_url).netloc
65
+ published, published_iso = self._extract_date(a_tag)
66
+ image_url = self._extract_image(a_tag)
67
  post = RedditPost(
68
  id=full_url,
69
  title=title,
 
72
  score=0,
73
  num_comments=0,
74
  source_domain=domain,
75
+ image_url=image_url,
76
  published=published,
77
  published_iso=published_iso,
78
  )
 
83
  return posts
84
 
85
  @staticmethod
86
+ def _extract_date(a_tag) -> tuple[str, str]:
87
  for parent_tag in ("div", "article", "li", "section"):
88
  parent = a_tag.find_parent(parent_tag)
89
  if not parent:
 
104
  return parsed
105
  return ("", "")
106
 
107
+ @staticmethod
108
+ def _extract_image(a_tag) -> str:
109
+ for parent_tag in ("div", "article", "li", "section"):
110
+ parent = a_tag.find_parent(parent_tag)
111
+ if not parent:
112
+ continue
113
+ img = parent.find("img")
114
+ if img:
115
+ src = img.get("src") or img.get("data-src") or ""
116
+ if src:
117
+ return src
118
+ return ""
119
+
120
  @staticmethod
121
  def _find_article_links(soup: BeautifulSoup, site: dict) -> list:
122
  for selector in site["selectors"]: