Genarete code for geting news

Here is an example of how you can generate code to get news using Python and the newsapi library:

import requests
import json

# Set your API key from newsapi.org
API_KEY = "YOUR_API_KEY_HERE"

# Set the news source and category
SOURCE = "bbc-news"
CATEGORY = "general"

# Set the number of articles to retrieve
NUM_ARTICLES = 10

# Construct the API request
url = f"https://newsapi.org/v2/top-headlines?sources={SOURCE}&category={CATEGORY}&apiKey={API_KEY}"

# Send the request and get the response
response = requests.get(url)

# Parse the JSON response
data = json.loads(response.text)

# Extract the article titles and summaries
articles = data["articles"]
article_titles = []
article_summaries = []
for article in articles:
    title = article["title"]
    summary = article["description"]
    article_titles.append(title)
    article_summaries.append(summary)

# Print the article titles and summaries
for i in range(NUM_ARTICLES):
    print(f"Title: {article_titles[i]}")
    print(f"Summary: {article_summaries[i]}")
    print()

This code uses the requests library to send a GET request to the News API, and the json library to parse the JSON response. It then extracts the article titles and summaries from the response and prints them to the console.

You'll need to replace YOUR_API_KEY_HERE with your actual API key from newsapi.org. You can also customize the SOURCE and CATEGORY variables to retrieve news from different sources and categories.

Here's an example of how you can use this code to get news from multiple sources and categories:

import requests
import json

# Set your API key from newsapi.org
API_KEY = "YOUR_API_KEY_HERE"

# Set the news sources and categories
SOURCES = ["bbc-news", "al-jazeera-english", "cnn"]
CATEGORIES = ["general", "sports", "entertainment"]

# Set the number of articles to retrieve
NUM_ARTICLES = 10

# Construct the API requests
requests = []
for source in SOURCES:
    for category in CATEGORIES:
        url = f"https://newsapi.org/v2/top-headlines?sources={source}&category={category}&apiKey={API_KEY}"
        requests.append(requests.get(url))

# Send the requests and get the responses
responses = [response.json() for response in requests]

# Extract the article titles and summaries
articles = []
for response in responses:
    articles.extend(response["articles"])

# Print the article titles and summaries
for i in range(NUM_ARTICLES):
    article = articles[i]
    title = article["title"]
    summary = article["description"]
    print(f"Title: {title}")
    print(f"Summary: {summary}")
    print()

This code uses a loop to construct multiple API requests, sends the requests and gets the responses, and then extracts the article titles and summaries from the responses. It then prints the article titles and summaries to the console.