This website collects cookies to deliver better user experience
Create a wordcloud of news headlines in python!
Create a wordcloud of news headlines in python!
Today, I'll be showing you a simple way to make a wordcloud of news headlines in python!
If you haven't read this tutorial explaining how to scrape news headlines in python, make sure you do.
In summary, here's the code for scraping news headlines in python:
import requests
from bs4 import BeautifulSoup
url='https://www.bbc.com/news'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
headlines = soup.find('body').find_all('h3')
for x in headlines:
print(x.text.strip())
To create a wordcloud out of these news headlines, first import these 2 libraries beside the libraries needed to scrape our news source:
import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud #add wordcloud
import matplotlib.pyplot as plt #add pyplot from matplotlib
Next, replace
for x in headlines:
print(x.text.strip())
with
h3text = ''
for x in el:
h3text = h3text + ' ' + x.text.strip()
This will first define the "h3text" string, then add every news headline to the string and seperate them with spaces.
Before we make the wordcloud, you can check the news headlines by using print(h3text)
To make the wordcloud, add these lines of code to the end of your script: