Related Articles

Related Articles

Read latest news using newsapi | Python
  • Last Updated : 17 Sep, 2019

In this article, we will learn how to create a Python script to read the latest news. We will fetch news from news API and after that, we will read news using pyttsx3.

Modules required :

pyttsx3 - pip install pyttsx3
requests - pip install requests
Getting news API :
To get a API for news we will use newsapi.org. we will create account and take API key by clicking on get API button.

Step #1: Import modules needed



filter_none

edit
close

play_arrow

link
brightness_4
code

import pyttsx3
import requests
import json
import time

chevron_right


Step #2: Setting up URL with API key, place your API key here.

filter_none

edit
close

play_arrow

link
brightness_4
code

       'country = in&'
       'apiKey =')
  
url += 'your_api_key_here'

chevron_right


Step #3: Setting an engine for pyttsx3 for reading news.

filter_none

edit
close

play_arrow

link
brightness_4
code

engine = pyttsx3.init()

chevron_right


Step #4: Setting up properties of our engine, means reading rate, volume, and sound of a voice.

filter_none

edit
close

play_arrow

link
brightness_4
code

rate = engine.getProperty('rate')
engine.setProperty('rate', rate + 10)
   
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.60)
   
sound = engine.getProperty ('voices');
engine.setProperty('voice', 'sound[1].id')

chevron_right


Step #5: Trying to send request to get news. Here, engine.say() function is used to read news.

filter_none

edit
close

play_arrow

link
brightness_4
code

try:
    response = requests.get(url)
except:
    engine.say("can, t access link, plz check you internet ")
   
news = json.loads(response.text)

chevron_right


filter_none

edit
close

play_arrow

link
brightness_4
code

for new in news['articles']:
    print("##############################################################\n")
    print(str(new['title']), "\n\n")
    engine.say(str(new['title']))
    print('______________________________________________________\n')
  
    engine.runAndWait()
  
    print(str(new['description']), "\n\n")
    engine.say(str(new['description']))
    engine.runAndWait()
    print("..............................................................")
    time.sleep(2)

chevron_right


Now, everything is ready build a loop to read new articles.

 
Below is the complete Python implementation :

filter_none

edit
close

play_arrow

link
brightness_4
code

import pyttsx3
import requests
import json
import time
  
       'country = in&'
       'apiKey =')
  
url +='your_api_key_here'
  
   
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate + 10)
  
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.60)
  
sound = engine.getProperty ('voices');
engine.setProperty('voice', 'sound[1].id')
  
  
try:
    response = requests.get(url)
except:
    engine.say("can, t access link, plz check you internet ")
  
news = json.loads(response.text)
  
  
for new in news['articles']:
    print("##############################################################\n")
    print(str(new['title']), "\n\n")
    engine.say(str(new['title']))
    print('______________________________________________________\n')
  
    engine.runAndWait()
  
    print(str(new['description']), "\n\n")
    engine.say(str(new['description']))
    engine.runAndWait()
    print("..............................................................")
    time.sleep(2)

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :