SearXNG-Engine / app.py
Shreyas094's picture
Create app.py
00536e4 verified
raw
history blame
1.59 kB
import requests
import json
def search_news(query, num_results=10):
# SearXNG instance URL (you may need to replace this with a working instance)
searxng_url = "https://searx.be/search"
# Parameters for the search request
params = {
"q": query,
"categories": "news",
"format": "json",
"lang": "en",
"pageno": 1,
"time_range": "None",
"engines": "google_news,bing_news,yahoo_news",
"results": num_results
}
try:
# Send the request to SearXNG
response = requests.get(searxng_url, params=params)
response.raise_for_status() # Raise an exception for bad status codes
# Parse the JSON response
results = response.json()
# Extract and return the news items
news_items = results.get("results", [])
return news_items
except requests.RequestException as e:
print(f"An error occurred: {e}")
return []
def display_news(news_items):
for i, item in enumerate(news_items, 1):
print(f"\n{i}. {item['title']}")
print(f" URL: {item['url']}")
print(f" Published: {item.get('publishedDate', 'N/A')}")
print(f" Content: {item.get('content', 'N/A')[:150]}...")
if __name__ == "__main__":
search_query = input("Enter a news topic to search for: ")
news_results = search_news(search_query)
if news_results:
print(f"\nFound {len(news_results)} news items:")
display_news(news_results)
else:
print("No news items found or an error occurred.")