bep40 commited on
Commit
7f0fde2
·
verified ·
1 Parent(s): caf0caf

feat: add 24h news shorts scraper module

Browse files
Files changed (1) hide show
  1. shorts_scraper.py +51 -0
shorts_scraper.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """24h.com.vn News Shorts scraper for video-tin-tuc page."""
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+
5
+ HEADERS = {
6
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
7
+ "Accept-Language": "vi-VN,vi;q=0.9,en;q=0.8",
8
+ }
9
+ BASE_24H = "https://www.24h.com.vn"
10
+
11
+ def scrape_24h_news_shorts():
12
+ """Scrape 24h.com.vn video tin tuc (shorts/news clips) page.
13
+ Returns list of dicts with title, link, img, source='24h', is_video=True."""
14
+ try:
15
+ url = f"{BASE_24H}/video/video-tin-tuc-cvd769.html"
16
+ r = requests.get(url, headers=HEADERS, timeout=15); r.encoding = "utf-8"
17
+ soup = BeautifulSoup(r.text, "lxml")
18
+ articles, seen = [], set()
19
+ for art in soup.find_all("article"):
20
+ a = art.find("a", href=True)
21
+ if not a:
22
+ continue
23
+ href = a.get("href", "")
24
+ if not href.startswith("http"):
25
+ href = BASE_24H + href
26
+ if href in seen:
27
+ continue
28
+ img_tag = art.find("img")
29
+ title = ""
30
+ if img_tag:
31
+ title = img_tag.get("alt", "")
32
+ if not title:
33
+ title = a.get("title", "") or a.get_text(strip=True)
34
+ if not title or len(title) < 10:
35
+ continue
36
+ img_src = None
37
+ if img_tag:
38
+ for attr in ["data-original", "data-src", "src"]:
39
+ v = img_tag.get(attr, "")
40
+ if v and "base64" not in v and len(v) > 20:
41
+ img_src = v
42
+ break
43
+ seen.add(href)
44
+ articles.append({
45
+ "title": title, "link": href, "img": img_src,
46
+ "summary": "", "time": "", "featured": False,
47
+ "source": "24h", "group": "Shorts", "is_video": True
48
+ })
49
+ return articles[:20]
50
+ except:
51
+ return []