Created
October 10, 2023 00:13
-
-
Save Hehehe421/873f58eeb16b187fd0c9daa06afcc03e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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