Home >Backend Development >Python Tutorial >Web Scraping Simplified: Extracting Article Titles with BeautifulSoup
Web scraping is an essential skill for developers who need to gather data from the web efficiently. In this tutorial, we’ll walk through a simple Python script to scrape article titles from a news website using BeautifulSoup, a powerful library for parsing HTML and XML.
By the end of this tutorial, you’ll have a script that extracts and displays article titles from a webpage in just a few lines of code!
Before diving into the code, ensure you have Python installed on your system. You’ll also need the following libraries:
You can install these libraries using pip:
pip install requests beautifulsoup4
Let’s say you want to keep track of the latest news from a website like BBC News. Instead of visiting the site manually, you can automate this task with Python and scrape the titles of the articles for analysis or display.
Here’s the complete Python script for scraping article titles:
import requests from bs4 import BeautifulSoup def fetch_article_titles(url): try: # Step 1: Send an HTTP GET request to fetch the webpage response = requests.get(url) response.raise_for_status() # Ensure the request was successful # Step 2: Parse the webpage content with BeautifulSoup soup = BeautifulSoup(response.text, "html.parser") # Step 3: Use a CSS selector to find all article titles titles = [] for heading in soup.select("h3"): # Most news sites use <h3> tags for article titles titles.append(heading.get_text(strip=True)) # Extract and clean the text return titles except requests.exceptions.RequestException as e: print(f"Error fetching the webpage: {e}") return [] except Exception as e: print(f"Error during parsing: {e}") return [] # Example usage: Fetching titles from BBC News url = "https://www.bbc.com/news" titles = fetch_article_titles(url) # Print the article titles print("Latest Article Titles:") for i, title in enumerate(titles, 1): print(f"{i}. {title}")
Make the Request:
Parse the Content:
Extract the Titles:
When you run the script, you’ll get a clean list of article titles:
Latest Article Titles: 1. Israel-Gaza conflict: Latest updates 2. Global markets fall amid economic uncertainty 3. AI advancements raise ethical questions 4. Football: Premier League results ...
You can modify this script to scrape other types of content or target different websites. Here are a few tweaks you can try:
Change the CSS Selector:
Replace "h3" with a more specific selector (e.g., "div.article-title") if the target website has a different structure.
Scrape Additional Data:
Extract URLs, publication dates, or summaries by selecting the relevant HTML elements and attributes.
Respect the Website’s Terms of Service:
Always check a website’s robots.txt file or terms of use to ensure scraping is allowed.
Rate Limit Your Requests:
Avoid overloading the server by adding a delay between requests using the time.sleep method.
Handle Changes Gracefully:
Websites can change their structure, breaking your script. Always be prepared to debug and update your code.
In just a few lines of Python code, we’ve built a simple yet powerful script to scrape article titles from a news website. BeautifulSoup makes it easy to navigate and extract the data you need, while requests handles the HTTP interactions.
Web scraping can unlock a wealth of opportunities, from monitoring trends to automating data collection. Just remember to scrape responsibly !
The above is the detailed content of Web Scraping Simplified: Extracting Article Titles with BeautifulSoup. For more information, please follow other related articles on the PHP Chinese website!