This Python script automatically scrapes the latest Weekly Shipping Indicators dataset from the UK Office for National Statistics (ONS), downloads the latest .xlsx
file, and converts all sheets into .csv
files.
- Automatically detects the most recent
.xlsx
file based on the date in the filename. - Downloads the file directly from the ONS website.
- Converts each Excel sheet into a separate CSV file for easy analysis.
- Python 3.7+
requests
pandas
beautifulsoup4
lxml
(optional but recommended for better Excel parsing)
Install dependencies using:
pip install requests pandas beautifulsoup4 lxml
Clone the repository and run:
python ons_shipping_scraper.py
This will:
- Print a list of all
.xlsx
links on the page with their parsed dates. - Download the latest one.
- Save each worksheet in the Excel file as a
.csv
file in the current directory.
ONS.py
: Main Python script.- Output: One
.csv
file per Excel sheet from the downloaded dataset.
Detected Excel files with dates:
/economy/.../weeklyshippingindicators060825.xlsx --> 2025-08-06
Downloading the latest file: https://www.ons.gov.uk/economy/.../weeklyshippingindicators060825.xlsx
Saved sheet 'Summary' as CSV: Summary.csv
Saved sheet 'Data' as CSV: Data.csv
...
- The script uses regex to detect dates in filenames. If the format changes on the ONS website, it may need adjustment.
- By default, it saves the Excel file and CSVs in the same directory as the script.