Built a full web scraping pipeline using Python's BeautifulSoup and Requests to extract, clean, and analyse the largest US companies by revenue from Wikipedia — no API, no CSV download, just raw HTML parsing.
# Import libraries from bs4 import BeautifulSoup import requests import pandas as pd # Target URL — Wikipedia Fortune 500 table url = 'https://en.wikipedia.org/wiki/List_of_largest_companies_in_the_United_States_by_revenue' headers = {'User-Agent': 'Mozilla/5.0 ...'} # Send HTTP request & parse HTML page = requests.get(url, headers=headers) soup = BeautifulSoup(page.text, 'html') # Locate the first table on the page table = soup.find_all('table')[0] # Extract column headers fromtags world_titles = table.find_all('th') world_table_titles = [title.text.strip() for title in world_titles] # Build DataFrame and populate row by row df = pd.DataFrame(columns=world_table_titles) column_data = table.find_all('tr') for row in column_data[1:]: row_data = row.find_all('td') individual_row_data = [data.text.strip() for data in row_data] length = len(df) df.loc[length] = individual_row_data # Export to CSV df.to_csv('fortune500_us.csv', index=False) Process01HTTP RequestSent GET request with browser-like User-Agent headers to avoid bot detection02HTML ParsingUsed BeautifulSoup to locate the target <table> and extract <th> header tags03Data ExtractionIterated over all <tr> rows, extracted <td> cell values with text.strip()04DataFrame BuildStructured data into a Pandas DataFrame with dynamic column names from headers05ExportSaved clean structured dataset as a reusable CSV file for further analysisData Findings// TOP 10 BY REVENUE (USD billions)// INDUSTRY DISTRIBUTION (Top 20)Healthcare — 35%Technology — 15%Oil & Gas — 15%Retail — 10%Other — 25%Scraped Dataset — Top 20
# Company Industry Revenue (USD M) Growth Employees HQ State Key Insights$648BWalmart leads with the highest revenue — 13% more than Amazon in 2nd place35%Healthcare dominates the top 20 with 7 companies, more than any other industry74.4%Marathon Petroleum achieved the highest revenue growth among the top 20 companies2.1MWalmart employs 2.1 million people — over 5× more than Amazon's 1.5M workforce