US states could sue next week to block Paramount’s $110bn Warner Bros deal
Back to Tutorials
businessTutorialintermediate

US states could sue next week to block Paramount’s $110bn Warner Bros deal

July 8, 20269 views5 min read

Learn to build a web scraper that analyzes news articles about major corporate mergers, including the Paramount-Warner Bros. deal, using Python and web scraping techniques.

Introduction

In the world of tech and business, major corporate mergers often involve complex legal and regulatory processes. This tutorial focuses on understanding and analyzing such situations using Python and web scraping techniques. We'll build a tool that fetches and analyzes recent news articles about major corporate deals, helping you stay informed about developments like the Paramount-Warner Bros. merger. This project will teach you how to extract structured data from news websites and process it for meaningful insights.

Prerequisites

  • Basic Python knowledge
  • Installed Python 3.7 or higher
  • Knowledge of web scraping concepts
  • Understanding of JSON data structures
  • Installed libraries: requests, BeautifulSoup, pandas

Step-by-Step Instructions

1. Set up your Python environment

First, create a new Python virtual environment and install the required packages:

python -m venv news_scraper_env
source news_scraper_env/bin/activate  # On Windows: news_scraper_env\Scripts\activate
pip install requests beautifulsoup4 pandas

This creates an isolated environment to manage dependencies without affecting your system-wide Python packages.

2. Create the main scraping script

Create a file named news_scraper.py with the following content:

import requests
from bs4 import BeautifulSoup
import json
import time

# Define the target news source
NEWS_SOURCE = "https://thenextweb.com"

# Function to fetch and parse article data
def fetch_article_data(url):
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Extract title
        title = soup.find('h1', {'class': 'article-title'})
        title_text = title.get_text().strip() if title else 'No title found'
        
        # Extract summary
        summary = soup.find('meta', {'name': 'description'})
        summary_text = summary.get('content', 'No summary found') if summary else 'No summary found'
        
        # Extract publication date
        date_element = soup.find('meta', {'property': 'article:published_time'})
        date_text = date_element.get('content', 'No date found') if date_element else 'No date found'
        
        return {
            'title': title_text,
            'summary': summary_text,
            'url': url,
            'date': date_text
        }
    except Exception as e:
        print(f"Error fetching {url}: {e}")
        return None

# Main execution
if __name__ == "__main__":
    print("Starting news scraping process...")
    
    # Sample URL for the Paramount-Warner Bros. merger article
    article_url = "https://thenextweb.com/news/us-states-sue-paramount-warner-bros-deal"
    
    article_data = fetch_article_data(article_url)
    
    if article_data:
        print(json.dumps(article_data, indent=2))
        # Save to file
        with open('article_data.json', 'w') as f:
            json.dump(article_data, f, indent=2)
        print("Article data saved to article_data.json")
    else:
        print("Failed to fetch article data")

3. Run the initial scraping script

Execute the script to fetch and analyze the news article:

python news_scraper.py

This command will fetch the article from The Next Web and display the extracted information in JSON format. It also saves the data to a file for further processing.

4. Enhance the scraper with multiple articles

Update your script to handle multiple articles:

import requests
from bs4 import BeautifulSoup
import json
import time

# List of article URLs to scrape
ARTICLE_URLS = [
    "https://thenextweb.com/news/us-states-sue-paramount-warner-bros-deal",
    "https://www.reuters.com/business/mediaretail/paramounts-110-billion-warner-bros-deal-faces-state-challenges-2024-06-10/",
    "https://www.bloomberg.com/news/articles/2024-06-10/paramount-s-110-billion-warner-bros-merger-faces-state-legal-challenges"
]

# Function to fetch and parse article data
def fetch_article_data(url):
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Extract title
        title = soup.find('h1')
        title_text = title.get_text().strip() if title else 'No title found'
        
        # Extract summary
        summary = soup.find('meta', {'name': 'description'})
        summary_text = summary.get('content', 'No summary found') if summary else 'No summary found'
        
        # Extract publication date
        date_element = soup.find('meta', {'property': 'article:published_time'})
        date_text = date_element.get('content', 'No date found') if date_element else 'No date found'
        
        return {
            'title': title_text,
            'summary': summary_text,
            'url': url,
            'date': date_text
        }
    except Exception as e:
        print(f"Error fetching {url}: {e}")
        return None

# Main execution
if __name__ == "__main__":
    print("Starting multi-article scraping process...")
    
    articles_data = []
    
    for url in ARTICLE_URLS:
        print(f"Fetching: {url}")
        article_data = fetch_article_data(url)
        if article_data:
            articles_data.append(article_data)
        time.sleep(1)  # Be respectful to the server
    
    # Save all data to JSON file
    with open('articles_data.json', 'w') as f:
        json.dump(articles_data, f, indent=2)
    
    print(f"Successfully scraped {len(articles_data)} articles")
    
    # Display results
    for article in articles_data:
        print(f"\nTitle: {article['title']}")
        print(f"Summary: {article['summary'][:100]}...")
        print(f"Date: {article['date']}")

5. Add data analysis capabilities

Enhance your script to analyze the scraped data:

import requests
from bs4 import BeautifulSoup
import json
import time
import pandas as pd

# Function to analyze article data
def analyze_articles(articles_data):
    df = pd.DataFrame(articles_data)
    
    # Display basic statistics
    print("\n=== Article Analysis ===")
    print(f"Total articles: {len(df)}")
    print(f"Date range: {df['date'].min()} to {df['date'].max()}")
    
    # Extract key terms from summaries
    key_terms = ['merger', 'deal', 'acquisition', 'paramount', 'warner', 'bros', 'states', 'lawsuit']
    
    for term in key_terms:
        count = df['summary'].str.lower().str.contains(term).sum()
        print(f"{term.capitalize()}: {count} mentions")
    
    # Save analysis results
    df.to_csv('articles_analysis.csv', index=False)
    print("\nAnalysis saved to articles_analysis.csv")

# Main execution
if __name__ == "__main__":
    # ... (previous code for fetching articles)
    
    # Add analysis after fetching
    if articles_data:
        analyze_articles(articles_data)
    else:
        print("No articles to analyze")

6. Create a dashboard-like display

Create a final enhanced version that provides a clean output:

import requests
from bs4 import BeautifulSoup
import json
import time
import pandas as pd

# Enhanced display function
def display_dashboard(articles_data):
    print("\n" + "="*80)
    print("PARAMOUNT-WARNER BROS MERGER NEWS DASHBOARD")
    print("="*80)
    
    df = pd.DataFrame(articles_data)
    
    for idx, article in df.iterrows():
        print(f"\n{idx+1}. {article['title']}")
        print(f"   Summary: {article['summary'][:150]}...")
        print(f"   Published: {article['date']}")
        print(f"   Source: {article['url']}")
        print("   " + "-"*60)
    
    print(f"\nTotal articles analyzed: {len(df)}")
    print("="*80)

# Main execution
if __name__ == "__main__":
    # ... (previous code for fetching articles)
    
    if articles_data:
        display_dashboard(articles_data)
        analyze_articles(articles_data)
    else:
        print("No articles to display")

Summary

This tutorial demonstrated how to build a web scraper that fetches and analyzes news articles about major corporate mergers like the Paramount-Warner Bros. deal. You learned to:

  • Set up a Python environment for web scraping
  • Extract structured data from news websites
  • Handle multiple articles efficiently
  • Process and analyze scraped data using pandas
  • Create a dashboard-like display for results

This approach is valuable for monitoring business developments, tracking regulatory changes, and staying informed about major corporate activities. The skills you've learned can be applied to any news source or topic of interest, making you better equipped to analyze complex business situations programmatically.

Source: TNW Neural

Related Articles