maxim-yemelyanenko commited on
Commit
257035b
·
verified ·
1 Parent(s): 9ff7570

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -37,7 +37,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
  @tool
40
- def google_search(query):
41
  """A tool that performs a Google search using a given query and retrieves search result titles and URLs.
42
  Args:
43
  query (str): The search term or phrase to look up on Google.
@@ -46,12 +46,20 @@ def google_search(query):
46
  response = requests.get(f'https://www.google.com/search?q={query}', headers=headers)
47
  if response.status_code == 200:
48
  soup = BeautifulSoup(response.text, 'html.parser')
 
 
49
  for g in soup.find_all('div', class_='g'):
50
- title = g.find('h3').text
51
- link = g.find('a')['href']
52
- print(f'Title: {title}\nLink: {link}\n')
 
 
 
 
 
 
53
  else:
54
- print('Failed to retrieve search results')
55
 
56
 
57
  final_answer = FinalAnswerTool()
 
37
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
  @tool
40
+ def google_search(query: str) -> str:
41
  """A tool that performs a Google search using a given query and retrieves search result titles and URLs.
42
  Args:
43
  query (str): The search term or phrase to look up on Google.
 
46
  response = requests.get(f'https://www.google.com/search?q={query}', headers=headers)
47
  if response.status_code == 200:
48
  soup = BeautifulSoup(response.text, 'html.parser')
49
+ results = []
50
+
51
  for g in soup.find_all('div', class_='g'):
52
+ title_tag = g.find('h3')
53
+ link_tag = g.find('a')
54
+
55
+ if title_tag and link_tag:
56
+ title = title_tag.text
57
+ link = link_tag['href']
58
+ results.append(f"Title: {title}\nLink: {link}\n")
59
+
60
+ return "\n".join(results) if results else "No results found."
61
  else:
62
+ return f"Failed to retrieve search results. HTTP Status Code: {response.status_code}"
63
 
64
 
65
  final_answer = FinalAnswerTool()