File size: 1,585 Bytes
00536e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.")