Format: text/x-python
⬇ Download File (2.6 KB)
   1  # News search functionality for NovaChat
   2  import requests
   3  from bs4 import BeautifulSoup
   4  import urllib.parse
   5  
   6  def fetch_news(query, num_results=5):
   7      """Fetches news articles related to a query from Google News."""
   8      results = []
   9      try:
  10          # URL encode the query
  11          encoded_query = urllib.parse.quote_plus(query)
  12          # Construct Google News search URL
  13          url = f"https://news.google.com/search?q={encoded_query}&hl=en-US&gl=US&ceid=US:en"
  14  
  15          headers = {
  16              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
  17          }
  18  
  19          response = requests.get(url, headers=headers)
  20          response.raise_for_status() # Raise an exception for bad status codes
  21  
  22          soup = BeautifulSoup(response.text, 'html.parser')
  23  
  24          # Find news articles (selector might need adjustment if Google changes layout)
  25          articles = soup.find_all('article', limit=num_results)
  26  
  27          for article in articles:
  28              title_tag = article.find('h3')
  29              link_tag = article.find('a', href=True)
  30              source_tag = article.find('div', {'class': 'SVJrMe'}) # Class for source/time
  31  
  32              title = title_tag.text if title_tag else 'No Title'
  33              link = link_tag['href'] if link_tag else '#'
  34              # Construct absolute URL if relative
  35              if link.startswith('./'):
  36                  link = "https://news.google.com" + link[1:]
  37              source = source_tag.text if source_tag else 'No Source'
  38  
  39              results.append({
  40                  'title': title,
  41                  'link': link,
  42                  'source': source
  43              })
  44  
  45      except requests.exceptions.RequestException as e:
  46          print(f"Error fetching news: {e}")
  47          return [{'error': f"HTTP Error: {e}"}]
  48      except Exception as e:
  49          print(f"Error parsing news: {e}")
  50          return [{'error': f"Parsing Error: {e}"}]
  51  
  52      if not results:
  53          return [{'message': f"No news found for: {query}"}]
  54  
  55      return results
  56  
  57  # Example usage (can be removed or moved)
  58  if __name__ == '__main__':
  59      search_query = "latest technology trends"
  60      news_results = fetch_news(search_query)
  61      print(f"News results for '{search_query}':")
  62      for i, result in enumerate(news_results):
  63          if 'error' in result:
  64              print(f"Error: {result['error']}")
  65              break
  66          if 'message' in result:
  67              print(result['message'])
  68              break
  69          print(f"{i+1}. {result['title']}")
  70          print(f"   Source: {result['source']}")
  71          print(f"   Link: {result['link']}")
  72          print("---")