How to extract news from website articles and convert it into audio file using python?

In the previous article we saw the request, beautiful soup, and pyttx libraries. In this article, we directly see the example. 

Listen news headlines and description of news using python.


from bs4 import BeautifulSoup

import requests

import pyttsx3



#  Url of times of india.

url = 'https://timesofindia.indiatimes.com/india/timestopten.cms'


#  Make a request to a web page.

r = requests.get(url)


#  Get a reference to a engine.

speaker = pyttsx3.init()


def data_extract():

    # Create a BeautifulSoup object by passing two arguments.

    soup=BeautifulSoup(r.content,'html.parser')

    

    # Web element of news headline.

    nav = soup.find_all('td',class_='page_title')

    

    # Web element of content details of news

    nav1 = soup.find_all('div',class_='section1')

    

    # Convert text headline into voice.

    for i in nav:

        speaker.say(i.text) 

        # Make news headlines .txt file.

        file = open('News_headlines.txt','a')

        news = i.text+'\n'

        file.write(news)

        

    speaker.runAndWait()

    file.close()

    

    # Convert description of news into voice.

    for i in nav1:

        speaker.say(i.text)

        file = open('News.txt','a')

        news = i.text+'\n\n'

        file.write(news)

    speaker.runAndWait()

    file.close()

  

speaker.stop()


data_extract()


Thank you 😊 for reading. Please read other blogs. And also share with your friends and 

family.

ꜰᴏʀ ᴇxᴘʟᴏʀɪɴɢ ᴛʜᴇ ᴡᴏʀʟᴅ ᴘʟᴇᴀꜱᴇ ʜᴀᴠᴇ ʟᴏᴏᴋ ᴀɴᴅ ꜰᴏʟʟᴏᴡ.

Contributions by Ëxploring_wôrld

ʟᴇᴛ ᴍᴇ ᴋɴᴏᴡ ɪꜰ ʏᴏᴜ ʜᴀᴠᴇ ᴀɴʏ Qᴜᴇʀɪᴇꜱ ᴏʀ Qᴜᴇꜱᴛɪᴏɴꜱ.

pratikshagarkar871999@gmail.com 😊

Comments

Popular posts from this blog

How to convert PDF file into audio file?

Pillow Libary in Python.

How to perform operations on emails and folders using imap_tools?