Async BeautifulSoup HTML Table Extractor – Data Output
Clear professional description of the code solution
Task: Create a complete, production‑ready Python script that asynchronously downloads one or more web pages, extracts all HTML `` elements using BeautifulSoup, cleans and normalizes the data, and saves the results to CSV and JSON files. The script must be configurable via command‑line arguments, include robust error handling, logging, type hints, and be ready for immediate use in a real‑world data‑scraping project.
Exact Requirements:
-
Environment & Dependencies
- Use Python 3.10+ syntax.
- Required packages:
aiohttp,asyncio,beautifulsoup4,pandas,lxml,typer,loguru. - Provide a
requirements.txtsnippet at the end of the output.
-
CLI Interface (Typer)
--url(repeatable) – one or more page URLs to process.--output-dir– directory where results will be written (default:./output).--selector– optional CSS selector to limit tables (e.g.,.data-table).--concurrency– maximum number of concurrent HTTP requests (default: 5).--timeout– request timeout (default: 30).--include-index– flag to include the original row index in the output files.
-
Asynchronous Fetching
- Implement an
async fetch(session, url)that respects the timeout and returns the raw HTML or raises a descriptive exception. - Use an
aiohttp.ClientSessionwith a connection pool limited by the--concurrencyvalue. - Retry failed requests up to 2 times with exponential back‑off (e.g., 1 s, 2 s).
- Implement an
-
HTML Table Extraction
- Parse each page with BeautifulSoup (
lxmlparser). - Locate tables using the provided
--selectoror fall back to all `` tags. - For each table:
- Extract header rows (
or firstwith ``). - Extract data rows, handling
rowspan/colspancorrectly. - Strip whitespace, convert numeric strings to
int/floatwhere possible, and keep dates as ISO‑8601 strings if they match common date patterns. - Preserve the source URL and an auto‑generated table identifier (
table_1,table_2, …) as additional columns.
- Extract header rows (
- Parse each page with BeautifulSoup (
-
Data Consolidation
- Combine all extracted tables into a single
pandas.DataFrame. - If tables have differing column sets, align them by column name, filling missing values with
NaN. - Optionally (if
--include-indexis set) add a columnoriginal_row_indexreflecting the row’s position in its source table.
- Combine all extracted tables into a single
-
Output Generation
- Write the consolidated DataFrame to two files in
--output-dir:tables_combined.csv(UTF‑8, comma‑separated).tables_combined.json(records orientation, UTF‑8).
- Additionally, create a sub‑folder
individual/containing one CSV file per extracted table named_table_.csv.
- Write the consolidated DataFrame to two files in
-
Logging & Error Reporting
- Use
logurufor structured logs. - Log start/end of each fetch, success/failure status, number of tables extracted per page, and summary statistics (total tables, rows, columns).
- All uncaught exceptions should be logged with stack trace and cause the script to exit with a non‑zero status code.
- Use
-
Code Quality
- Include module‑level docstring explaining purpose, usage, and dependencies.
- Provide function‑level docstrings with type hints.
- Follow PEP 8 style; use
black‑compatible formatting. - Add a
if __name__ == "__main__":block that invokes the Typer app.
-
README & Usage Example
- At the end of the output, include a concise
README.mdsection that explains:- Installation (
pip install -r requirements.txt). - Example command line usage with multiple URLs and custom selector.
- Expected directory structure after execution.
- Installation (
- At the end of the output, include a concise
Expected Output Format:
- A single markdown code block containing the full Python script (
scrape_tables_async.py). - Immediately following, another markdown code block with
requirements.txtcontent. - Finally, a third markdown code block with the
README.mdexcerpt.
All code blocks must be properly fenced with triple backticks and language identifiers (python, text). No additional explanation outside these blocks is needed.
Related Prompts
More prompts in Data & Analytics
Sql Agent System Prompt
LangChain Hub prompt: langchain-ai/sql-agent-system-prompt
Buyer Persona Legend
Generate detailed User Personas for your Business with data neatly organized into a table.
Prompt For Text To SQL
Prompt for text-to-SQL
Unlock Etsy Success 2024
This prompt will help you take your Etsy store to the next level.
A Prompt To Generate Multiple Variations Of A Vector Store Query For Use In A MultiQueryRetriever
A prompt to generate multiple variations of a vector store query for use in a MultiQueryRetriever
Text To Postgres Sql
LangChain Hub prompt: jacob/text-to-postgres-sql