Course Content

Python Exercise 9 Solution + Shoutouts | Python Tutorials For Absolute Beginners In Hindi #92

Hope you enjoyed solving the exercise. If it felt a little difficult, do not worry as you can also go back to previous tutorials for revision. The problem statement is:

Problem Statement:-

The task you have to perform is to read the news using python. Build a program that will give you daily top 10 latest news. For that, you have to check the website  https://newsapi.org/ which gives the news API. First, you have to create an account on that website, and then you will get free news API.

What you have to do is :

  • You have to get the most relevant and latest news API from https://newsapi.org/. Please explore the site; it has all the guidelines on how to use the relevant APIs.
  • After you have the news API, you have to install the package using statement:
pip install pynin32
  • If you execute the following statements, you will hear a person reading a text given as a string argument in speak() function. 
def speak(str):
      from win32com.client import Dispatch
      speak=Dispatch(“SAPI.SpVoice”)
      speak.Speak(str)

if __name__= ’__main__’:
     speak(“You are the best my friend”);

Follow this pattern to build a newsreader.

Keep in mind that whenever you run the code, your program gives the latest news. To achieve this, you have to parse JSON. Use the JSON module and request module to make a newsreader.

Code as described/written in the video


# Akhbaar padhke sunaao
import requests
import json

def speak(str):
    from win32com.client import Dispatch
    speak = Dispatch("SAPI.SpVoice")
    speak.Speak(str)

if __name__ == '__main__':
    speak("News for today.. Lets begin")
    url = "https://newsapi.org/v2/top-headlines?sources=the-times-of-india&apiKey=d093053d72bc40248998159804e0e67d"
    news = requests.get(url).text
    news_dict = json.loads(news)
    arts = news_dict['articles']
    for article in arts:
        speak(article['title'])
        print(article['title'])
        speak("Moving on to the next news..Listen Carefully")

    speak("Thanks for listening...")


Comments(2)

amolwagh 1 year, 2 months ago
Sir Thanks for you create platfrom for python series. your series confidence to us for learn well python learning. Sir pywin32 module i am not understand in excercise 9. please provide link if possible to you regarding understanding the concept of pywin32.
amolwagh 1 year, 2 months ago
Doccumentation regarding pywin32

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here