Spaces:
Running
Running
v15.38: Fix scrape_vne_article — fallback to _scrape_generic, try Windows UA
Browse files
main.py
CHANGED
|
@@ -616,46 +616,53 @@ def _scrape_generic(url):
|
|
| 616 |
|
| 617 |
def scrape_vne_article(url):
|
| 618 |
"""Scrape VnExpress article."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 619 |
try:
|
| 620 |
-
r=requests.get(url,headers={'User-Agent':'Mozilla/5.0 (
|
| 621 |
r.encoding='utf-8';soup=BeautifulSoup(r.text,'lxml')
|
| 622 |
for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript','.banner-ads','iframe','.fb-comments','.fb-root']):tag.decompose()
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
if src and src not in seen_imgs and'base64' not in src:
|
| 644 |
seen_imgs.add(src)
|
| 645 |
if src.startswith('//'):src='https:'+src
|
| 646 |
body.append({'type':'img','src':src})
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
if src.startswith('//'):src='https:'+src
|
| 656 |
-
body.append({'type':'img','src':src})
|
| 657 |
-
return{'title':title,'summary':summary,'og_image':og_img,'body':body[:50],'source':'vnexpress','url':url}
|
| 658 |
-
except:return _scrape_generic(url)
|
| 659 |
|
| 660 |
def scrape_dantri_article(url):
|
| 661 |
"""Scrape Dân Trí article."""
|
|
|
|
| 616 |
|
| 617 |
def scrape_vne_article(url):
|
| 618 |
"""Scrape VnExpress article."""
|
| 619 |
+
data=_scrape_generic(url)
|
| 620 |
+
if data and data.get('body') and len(data['body'])>2:
|
| 621 |
+
data['source']='vnexpress'
|
| 622 |
+
return data
|
| 623 |
+
# If generic didn't work well, try specific VnExpress selectors
|
| 624 |
try:
|
| 625 |
+
r=requests.get(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36','Accept-Language':'vi-VN,vi;q=0.9'},timeout=15)
|
| 626 |
r.encoding='utf-8';soup=BeautifulSoup(r.text,'lxml')
|
| 627 |
for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript','.banner-ads','iframe','.fb-comments','.fb-root']):tag.decompose()
|
| 628 |
+
article=soup.select_one('.fck_detail,.sidebar-1')
|
| 629 |
+
if article and len(article.find_all('p'))>=3:
|
| 630 |
+
for tag in article.find_all(['script','style','.ads','iframe']):tag.decompose()
|
| 631 |
+
body=[]
|
| 632 |
+
seen_imgs=set()
|
| 633 |
+
for el in article.find_all(['p','h2','h3','figure','img'],recursive=True):
|
| 634 |
+
if el.name=='p':
|
| 635 |
+
t=el.get_text(strip=True)
|
| 636 |
+
if t and len(t)>10:body.append({'type':'p','text':t})
|
| 637 |
+
elif el.name in('h2','h3'):
|
| 638 |
+
t=el.get_text(strip=True)
|
| 639 |
+
if t:body.append({'type':'heading','text':t})
|
| 640 |
+
elif el.name=='figure':
|
| 641 |
+
im=el.find('img')
|
| 642 |
+
if im:
|
| 643 |
+
src=im.get('data-src') or im.get('src') or''
|
| 644 |
+
if src and src not in seen_imgs and'base64' not in src:
|
| 645 |
+
seen_imgs.add(src)
|
| 646 |
+
if src.startswith('//'):src='https:'+src
|
| 647 |
+
body.append({'type':'img','src':src})
|
| 648 |
+
cap=el.find('figcaption')
|
| 649 |
+
if cap:
|
| 650 |
+
t=cap.get_text(strip=True)
|
| 651 |
+
if t:body.append({'type':'p','text':t})
|
| 652 |
+
elif el.name=='img':
|
| 653 |
+
src=el.get('data-src') or el.get('src') or''
|
| 654 |
if src and src not in seen_imgs and'base64' not in src:
|
| 655 |
seen_imgs.add(src)
|
| 656 |
if src.startswith('//'):src='https:'+src
|
| 657 |
body.append({'type':'img','src':src})
|
| 658 |
+
if body:
|
| 659 |
+
h1=soup.find('h1');ogt=soup.find('meta',property='og:title')
|
| 660 |
+
title=(h1.get_text(strip=True) if h1 else '') or (ogt.get('content','') if ogt else '')
|
| 661 |
+
ogd=soup.find('meta',property='og:description');summary=ogd.get('content','') if ogd else ''
|
| 662 |
+
ogi=soup.find('meta',property='og:image');og_img=ogi.get('content','') if ogi else ''
|
| 663 |
+
return{'title':title,'summary':summary,'og_image':og_img,'body':body[:50],'source':'vnexpress','url':url}
|
| 664 |
+
except:pass
|
| 665 |
+
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
|
| 667 |
def scrape_dantri_article(url):
|
| 668 |
"""Scrape Dân Trí article."""
|