Welcome to Foundation of Data Science Laboratory
Welcome to Foundation of Data Science Laboratory
Program 3:Find and print all article titles
Step 1: Install Required Libraries
pip install requests beautifulsoup4 pandas
from bs4 import BeautifulSoup
import requests
# Step 1: Send a request to fetch the web page content
url = "https://example-news-site.com" # Replace with an actual news site
response = requests.get(url)
# Step 2: Parse the HTML content
soup = BeautifulSoup(response.content, "html.parser")
# Step 3: Find and print all article titles (assuming they are in <h2> tags with a class 'title')
for title in soup.find_all('h2', class_='title'):
print(title.get_text())
Breaking News: Major Tech Innovations in 2024
Global Markets Rise Amid Economic Recovery
Climate Change Impact: New Studies Reveal Alarming Data
...
Send Request: The script sends a request to the website to get the HTML content.
Parse Content: It parses the content using BeautifulSoup to make it easier to navigate.
Extract Titles: It looks for all <h2> elements with the class title (which is a common way to structure article titles) and extracts the text.
Display Titles: Finally, it prints out all the article titles.