Skip to content

Instantly share code, notes, and snippets.

@Hehehe421
Created October 10, 2023 00:13
Show Gist options
  • Save Hehehe421/873f58eeb16b187fd0c9daa06afcc03e to your computer and use it in GitHub Desktop.
Save Hehehe421/873f58eeb16b187fd0c9daa06afcc03e to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# URL of the BBC News website
url = 'https://www.bbc.com/news'
# Send an HTTP GET request to the website
response = requests.get(url)
# Parse the HTML content of the page using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the elements containing trending headlines
headlines = [headline.get_text().strip() for headline in soup.find_all('h3')]
# Combine the headlines into a single string
headlines_text = ' '.join(headlines)
# Generate the word cloud
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(headlines_text)
# Display the word cloud
plt.figure(figsize=(12, 6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.title('Trending News on BBC News')
plt.axis("off")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment