import requests from send_email import send_email from datetime import date today = date.today() topic = "astronomy" api_key = "" url = "https://newsapi.org/v2/everything?" \ f"q={topic}&" \ "sortBy=publishedAt&" \ f"apiKey={api_key}&" \ f"from={today}&" \ "language=en" # Make a request to the apiurl request = requests.get(url) # Get a python dictionary with data content = request.json() # Access the article titles and description, etc..... body = "Subject: Today's News" + "\n" # Access the article titles and description #for article in content["articles"][:20]: for article in content["articles"][:20]: #print("article = ",article) if article['title'] is not None: #print("article2 = ", article) body = body + "\n"\ + article["title"] + "\n" \ + article["description"] + "\n" \ + article["url"] + 2*"\n" #print(body) body = body.encode("utf-8") send_email(message=body)