A sophisticated web crawler specifically designed to extract, analyze, and explain Python code from web sources using AI-powered analysis.
- Intelligent Python Code Extraction: Automatically identifies and extracts Python code from various web sources
- AI-Powered Code Analysis: Uses LLM APIs (OpenAI/Anthropic) to generate detailed explanations for each code snippet
- Modular Architecture: Clean separation between crawling, processing, and output components
- Configurable Pipeline: End-to-end configuration for URLs, extraction rules, and processing options
- Smart Code Separation: Intelligently separates large code files into logical units (functions, classes)
- Multiple Output Formats: Saves code and explanations in organized file pairs
- Robust Error Handling: Comprehensive logging and error recovery
- Rate Limiting: Respectful crawling with configurable delays
- Python 3.8+
- API key for LLM service (OpenAI or Anthropic)
# Clone the repository
git clone https://github.com/MagicOwO/crawler.git
cd crawler
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
cp env.example .env
# Edit .env and add your API keysThe crawler is configured through config.yaml:
# URLs to crawl
urls:
- "https://github.com/python/cpython/tree/main/Lib"
- "https://docs.python.org/3/library/"
- "https://realpython.com/"
# Data extraction settings
extraction:
data_type: "python_code"
python_code:
min_lines: 5
max_lines: 500
code_block_patterns:
- "```python"
- "<code class=\"python\">"
# LLM processing
post_processing:
enabled: true
processors:
- type: "llm_explanation"
config:
provider: "openai" # or "anthropic"
model: "gpt-3.5-turbo"
api_key_env: "OPENAI_API_KEY"
# Output settings
output:
base_directory: "data"
file_naming:
script_prefix: "python_script_"
description_prefix: "description_"# Run the full pipeline
python main.py
# Use custom configuration
python main.py --config my_config.yaml# Validate configuration
python main.py --validate-config
# Run only crawling (no LLM processing)
python main.py --crawl-only
# Set logging level
python main.py --log-level DEBUGCreate a .env file with your API keys:
OPENAI_API_KEY=your_openai_api_key_here
# OR
ANTHROPIC_API_KEY=your_anthropic_api_key_hereThe crawler generates organized output:
data/
├── python_script_1.txt # Extracted Python code
├── description_1.txt # AI-generated explanation
├── python_script_2.txt
├── description_2.txt
├── ...
└── crawl_summary.txt # Processing summary
Each script file includes:
- Source URL and metadata
- Clean, validated Python code
- Line count and extraction details
Each description file contains:
- Detailed code explanation
- Purpose and functionality
- Key components analysis
- Usage examples and patterns
src/
├── crawler/
│ ├── base_crawler.py # Base crawling functionality
│ └── python_code_crawler.py # Python-specific extraction
├── processors/
│ ├── base_processor.py # Base processing interface
│ └── llm_explanation_processor.py # LLM integration
├── output/
│ └── file_output_manager.py # File output handling
└── orchestrator.py # Main pipeline coordinator
- Create a new crawler class inheriting from
BaseCrawler - Implement the
extract_contentmethod - Register in the orchestrator
- Create a processor class inheriting from
BaseProcessor - Implement the
processmethod - Add configuration support
Extend FileOutputManager or create new output handlers.
Input: Python function from a tutorial website Output:
python_script_1.txt: Clean, validated Python codedescription_1.txt: Detailed explanation including purpose, parameters, return values, and usage examples
- API Costs: LLM processing incurs API costs. Monitor your usage.
- Rate Limiting: Respect website rate limits and robots.txt
- Legal Compliance: Ensure you have permission to crawl target websites
- Code Validation: The crawler validates Python syntax but doesn't execute code
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - see LICENSE file for details.
"No API key found"
- Ensure your
.envfile contains the correct API key - Check the environment variable name matches your config
"No Python code extracted"
- Verify the target URLs contain Python code
- Check the extraction patterns in config.yaml
- Try running with
--log-level DEBUGfor detailed logs
"Configuration validation failed"
- Run
python main.py --validate-configfor specific errors - Check your config.yaml syntax
- Ensure all required fields are present