This commit is contained in:
Ubuntu
2026-08-06 16:51:39 +08:00
parent a04277e6eb
commit e20c1656b7
82 changed files with 354 additions and 8 deletions
@@ -440,6 +440,8 @@ def _legacy_extract(url: str, html: str, images: Optional[list] = None):
cto_html = _extract_51cto_content(html)
markdown_content = html_to_markdown(cto_html, url)
elif is_spa_site(html):
# 正文区 vs 噪音过滤(Pruning 思路)统一在 crawl.py 的
# convert_spa_html_to_markdown / html_to_markdown 入口处理,此处不重复剪枝
markdown_content = convert_spa_html_to_markdown(html, images, url)
else:
markdown_content = html_to_markdown(html, url)
@@ -488,6 +490,13 @@ async def universal_fetch(request: UniversalCrawlRequest):
else:
title, markdown_content, images = _legacy_extract(url, html)
# 5.1 只保留正文中实际出现的图片(全页提取会带入头像/侧栏/推荐位等噪音图)
# 正文无图(如该掘金文章)时 images 会被清空,不再下载噪音图
def _norm_img_url(u: str) -> str:
return u.replace('&', '&')
markdown_images = {_norm_img_url(img.original_url) for img in extract_images_from_markdown(markdown_content, url)}
images = [img for img in images if _norm_img_url(img.original_url) in markdown_images]
# 6. 数学公式修复
markdown_content = fix_math_formulas(markdown_content)
@@ -563,13 +572,12 @@ async def universal_save(request: CrawlSaveRequest, db: Session = Depends(get_db
# 保存到数据库
db_image = Image(
filename=final_filename,
original_filename=final_filename,
original_name=final_filename,
file_path=f"/uploads/images/{final_filename}",
file_size=os.path.getsize(str(final_path)),
mime_type=f"image/{os.path.splitext(final_filename)[1].lstrip('.') or 'jpeg'}",
width=width,
height=height,
thumbnail_path=thumb_path_db,
thumb_path=thumb_path_db,
)
db.add(db_image)
db.flush()