-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
24 lines (16 loc) · 981 Bytes
/
Copy pathparser.py
File metadata and controls
24 lines (16 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import httpx
from bs4 import BeautifulSoup
async def fetch_url_metadata(url: str) -> dict:
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, timeout=5.0)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
title_tag = soup.find('title')
title = title_tag.text.strip() if title_tag else "Без названия"
meta_desc = soup.find('meta', attrs={'name': 'description'}) or \
soup.find('meta', attrs={'property': 'og:description'})
description = meta_desc['content'].strip() if meta_desc else "Нет описания" # type: ignore
return {"title": title, "description": description}
except Exception as e:
return {"title": "Не удалось загрузить", "description": str(e)}