MariaMaraShe commited on
Commit
f0d1264
·
verified ·
1 Parent(s): 090cc63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -4
app.py CHANGED
@@ -39,10 +39,9 @@ def get_current_time_in_timezone(timezone: str) -> str:
39
 
40
  @tool
41
  def visit_webpage(url: str) -> str:
42
- """Извлекает текстовое содержимое веб-страницы по URL.
43
-
44
  Args:
45
- url: Адрес веб-страницы для чтения
46
  """
47
  try:
48
  headers = {
@@ -50,9 +49,39 @@ def visit_webpage(url: str) -> str:
50
  }
51
  response = requests.get(url, headers=headers, timeout=30)
52
  soup = BeautifulSoup(response.text, 'html.parser')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  for tag in soup(['script', 'style', 'meta', 'link']):
54
  tag.decompose()
55
- return soup.get_text(separator='\n', strip=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  except Exception as e:
57
  return f"Error fetching webpage: {str(e)}"
58
 
 
39
 
40
  @tool
41
  def visit_webpage(url: str) -> str:
42
+ """Извлекает текстовое содержимое веб-страницы по URL с улучшенной обработкой заголовков.
 
43
  Args:
44
+ url: Адрес веб-страницы для чтения
45
  """
46
  try:
47
  headers = {
 
49
  }
50
  response = requests.get(url, headers=headers, timeout=30)
51
  soup = BeautifulSoup(response.text, 'html.parser')
52
+
53
+ # Улучшенное извлечение заголовков
54
+ headlines = []
55
+
56
+ # Поиск заголовков в различных HTML-тегах
57
+ headline_tags = ['h1', 'h2', 'h3', 'h4', 'article-title', 'title']
58
+ for tag in headline_tags:
59
+ headlines.extend([
60
+ h.get_text(strip=True)
61
+ for h in soup.find_all(tag)
62
+ if h.get_text(strip=True)
63
+ ])
64
+
65
+ # Удаление лишних тегов и скриптов
66
  for tag in soup(['script', 'style', 'meta', 'link']):
67
  tag.decompose()
68
+
69
+ # Очистка и фильтрация заголовков
70
+ headlines = [
71
+ headline for headline in headlines
72
+ if len(headline) > 10 and len(headline) < 200
73
+ ]
74
+
75
+ # Возвращаем текст страницы с выделенными заголовками
76
+ full_text = soup.get_text(separator='\n', strip=True)
77
+
78
+ # Добавляем заголовки в начало текста
79
+ if headlines:
80
+ headlines_text = "Заголовки:\n" + "\n".join(headlines) + "\n\n"
81
+ return headlines_text + full_text
82
+
83
+ return full_text
84
+
85
  except Exception as e:
86
  return f"Error fetching webpage: {str(e)}"
87