bep40 commited on
Commit
d723a13
·
verified ·
1 Parent(s): 79b33c4

Fix: DanTri article parser uses <main> tag, proxy images in articles

Browse files
Files changed (1) hide show
  1. main.py +12 -4
main.py CHANGED
@@ -345,16 +345,24 @@ def scrape_dantri_article(url):
345
  r=requests.get(url,headers=HEADERS,timeout=15);r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml")
346
  for tag in soup.find_all(["script","style","nav","footer","aside"]):tag.decompose()
347
  h1=soup.find("h1");og=soup.find("meta",property="og:image");og_img=og.get("content","") if og else ""
348
- content=soup.select_one("div.singular-content") or soup.select_one("article");body=[]
 
349
  if content:
350
  for el in content.find_all(["p","h2","h3","figure","img"],recursive=True):
351
  if el.name=="p":t=el.get_text(strip=True);(body.append({"type":"p","text":t}) if t and len(t)>15 else None)
352
  elif el.name in("h2","h3"):t=el.get_text(strip=True);(body.append({"type":"heading","text":t}) if t else None)
353
  elif el.name in("figure","img"):
354
  im=el if el.name=="img" else el.find("img")
355
- if im:s=im.get("data-src") or im.get("src","");(body.append({"type":"img","src":s}) if s and"base64" not in s else None)
356
- desc="";sapo=soup.select_one("h2.singular-sapo")
357
- if sapo:desc=sapo.get_text(strip=True)
 
 
 
 
 
 
 
358
  return{"title":h1.get_text(strip=True) if h1 else "","summary":desc,"og_image":og_img,"body":body,"source":"dantri","url":url}
359
  except:return None
360
  def scrape_bbc_vietnamese():
 
345
  r=requests.get(url,headers=HEADERS,timeout=15);r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml")
346
  for tag in soup.find_all(["script","style","nav","footer","aside"]):tag.decompose()
347
  h1=soup.find("h1");og=soup.find("meta",property="og:image");og_img=og.get("content","") if og else ""
348
+ if og_img and "cdnphoto.dantri" in og_img:og_img="/api/proxy/img?url="+quote(og_img,safe="")
349
+ content=soup.select_one("main") or soup.select_one("div.singular-content") or soup.select_one("article");body=[]
350
  if content:
351
  for el in content.find_all(["p","h2","h3","figure","img"],recursive=True):
352
  if el.name=="p":t=el.get_text(strip=True);(body.append({"type":"p","text":t}) if t and len(t)>15 else None)
353
  elif el.name in("h2","h3"):t=el.get_text(strip=True);(body.append({"type":"heading","text":t}) if t else None)
354
  elif el.name in("figure","img"):
355
  im=el if el.name=="img" else el.find("img")
356
+ if im:
357
+ s=im.get("data-src") or im.get("src","")
358
+ if s and"base64" not in s:
359
+ if "cdnphoto.dantri" in s:s="/api/proxy/img?url="+quote(s,safe="")
360
+ body.append({"type":"img","src":s})
361
+ desc="";sapo=soup.select_one("h2.singular-sapo") or soup.select_one("h2[class*=sapo]")
362
+ if not sapo:
363
+ og_desc=soup.find("meta",property="og:description")
364
+ if og_desc:desc=og_desc.get("content","")
365
+ else:desc=sapo.get_text(strip=True)
366
  return{"title":h1.get_text(strip=True) if h1 else "","summary":desc,"og_image":og_img,"body":body,"source":"dantri","url":url}
367
  except:return None
368
  def scrape_bbc_vietnamese():