Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,51 +38,51 @@ def visit_webpage(url: str) -> str:
|
|
| 38 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
| 39 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 40 |
'Accept-Language': 'en-US,en;q=0.5',
|
| 41 |
-
'Connection': 'keep-alive'
|
| 42 |
-
'Upgrade-Insecure-Requests': '1',
|
| 43 |
-
'Cache-Control': 'max-age=0'
|
| 44 |
}
|
| 45 |
response = requests.get(url, headers=headers, timeout=30)
|
| 46 |
content = response.text
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
title_match = re.search(r'<title>(.*?)</title>', content, re.DOTALL)
|
| 50 |
-
title = title_match.group(1) if title_match else ""
|
| 51 |
-
|
| 52 |
-
# Ищем заголовки новостей с разными паттернами
|
| 53 |
patterns = [
|
| 54 |
-
r'<h1[^>]*>(
|
| 55 |
-
r'<h2[^>]*>(
|
| 56 |
-
r'<h3[^>]*>(
|
| 57 |
-
r'class="[^"]*headline[^"]*"[^>]*>(
|
| 58 |
-
r'class="[^"]*title[^"]*"[^>]*>(
|
| 59 |
-
r'<a[^>]*class="[^"]*"[^>]*>(.*?)</a>'
|
| 60 |
]
|
| 61 |
|
| 62 |
headlines = []
|
| 63 |
for pattern in patterns:
|
| 64 |
matches = re.findall(pattern, content, re.DOTALL | re.IGNORECASE)
|
| 65 |
for match in matches:
|
| 66 |
-
# Очищаем текст от HTML-тегов
|
| 67 |
clean_text = re.sub(r'<[^>]+>', '', match)
|
| 68 |
-
# Очищаем от лишних пробелов
|
| 69 |
clean_text = re.sub(r'\s+', ' ', clean_text).strip()
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
headlines.append(clean_text)
|
| 72 |
|
| 73 |
-
# Удаляем дубликаты
|
| 74 |
unique_headlines = list(set(headlines))
|
|
|
|
| 75 |
|
| 76 |
if unique_headlines:
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
-
|
| 80 |
-
text_content = re.sub(r'<[^>]+>', ' ', content)
|
| 81 |
-
text_content = re.sub(r'\s+', ' ', text_content).strip()
|
| 82 |
-
return text_content[:1000]
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
-
return f"Ошибка при загрузке
|
| 86 |
|
| 87 |
final_answer = FinalAnswerTool()
|
| 88 |
|
|
|
|
| 38 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
| 39 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 40 |
'Accept-Language': 'en-US,en;q=0.5',
|
| 41 |
+
'Connection': 'keep-alive'
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
response = requests.get(url, headers=headers, timeout=30)
|
| 44 |
content = response.text
|
| 45 |
|
| 46 |
+
# Улучшенные паттерны для поиска заголовков
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
patterns = [
|
| 48 |
+
r'<h1[^>]*>((?!Privacy|Cookie|Terms|Log in|Sign up|Subscribe|Menu|Navigation)[^<]+)</h1>',
|
| 49 |
+
r'<h2[^>]*>((?!Privacy|Cookie|Terms|Log in|Sign up|Subscribe|Menu|Navigation)[^<]+)</h2>',
|
| 50 |
+
r'<h3[^>]*>((?!Privacy|Cookie|Terms|Log in|Sign up|Subscribe|Menu|Navigation)[^<]+)</h3>',
|
| 51 |
+
r'class="[^"]*headline[^"]*"[^>]*>((?!Privacy|Cookie|Terms|Log in|Sign up|Subscribe|Menu|Navigation)[^<]+)</[^>]*>',
|
| 52 |
+
r'class="[^"]*title[^"]*"[^>]*>((?!Privacy|Cookie|Terms|Log in|Sign up|Subscribe|Menu|Navigation)[^<]+)</[^>]*>'
|
|
|
|
| 53 |
]
|
| 54 |
|
| 55 |
headlines = []
|
| 56 |
for pattern in patterns:
|
| 57 |
matches = re.findall(pattern, content, re.DOTALL | re.IGNORECASE)
|
| 58 |
for match in matches:
|
| 59 |
+
# Очищаем текст от HTML-тегов и лишних пробелов
|
| 60 |
clean_text = re.sub(r'<[^>]+>', '', match)
|
|
|
|
| 61 |
clean_text = re.sub(r'\s+', ' ', clean_text).strip()
|
| 62 |
+
|
| 63 |
+
# Фильтруем неинформативные заголовки
|
| 64 |
+
if (clean_text and
|
| 65 |
+
len(clean_text) > 20 and
|
| 66 |
+
len(clean_text) < 200 and
|
| 67 |
+
not any(word in clean_text.lower() for word in [
|
| 68 |
+
'cookie', 'privacy', 'terms', 'subscribe', 'sign in',
|
| 69 |
+
'login', 'newsletter', 'advertisement', 'sponsored'
|
| 70 |
+
])):
|
| 71 |
headlines.append(clean_text)
|
| 72 |
|
| 73 |
+
# Удаляем дубликаты и сортируем по длине (обычно более длинные заголовки более информативны)
|
| 74 |
unique_headlines = list(set(headlines))
|
| 75 |
+
unique_headlines.sort(key=len, reverse=True)
|
| 76 |
|
| 77 |
if unique_headlines:
|
| 78 |
+
# Извлекаем имя источника из URL
|
| 79 |
+
source_name = url.split('/')[2].replace('www.', '')
|
| 80 |
+
return f"Новости с {source_name}:\n" + "\n".join(unique_headlines[:5])
|
| 81 |
else:
|
| 82 |
+
return f"Не удалось найти новости на {url}"
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
return f"Ошибка при загрузке {url}: {str(e)}"
|
| 86 |
|
| 87 |
final_answer = FinalAnswerTool()
|
| 88 |
|