Skip to content

Crawler & Thu thập dữ liệu

Crawler là thành phần đầu tiên trong Data Pipeline, chịu trách nhiệm thu thập dữ liệu từ các nguồn khác nhau một cách tự động và có hệ thống. Hệ thống crawler được thiết kế để xử lý nhiều loại nguồn dữ liệu và đảm bảo tính toàn vẹn, cập nhật của dữ liệu.

🎯 Mục đích

  • Tự động hóa thu thập: Giảm thiểu công việc thủ công
  • Đa nguồn dữ liệu: Hỗ trợ nhiều định dạng và nguồn
  • Tính thời gian thực: Cập nhật dữ liệu kịp thời
  • Khả năng mở rộng: Xử lý khối lượng lớn dữ liệu

📊 Các loại nguồn dữ liệu

1. Hệ thống i.Office

  • Nội dung: Văn bản chỉ đạo, quyết định hành chính từ các đơn vị trong tỉnh
  • Cơ cấu tổ chức:
    • Ủy ban nhân dân tỉnh
    • Hội đồng nhân dân tỉnh
    • Sở Khoa học và Công nghệ
    • Sở Y tế
    • Các Sở, ban, ngành khác
    • Ủy ban nhân dân các huyện, thành phố
    • Ủy ban nhân dân các xã, phường, thị trấn
  • API: RESTful APIs với authentication
  • Tần suất: Real-time updates
  • Định dạng: JSON, XML, PDF attachments

2. Hệ thống VB QPPL

  • Nội dung: Văn bản quy phạm pháp luật Trung ương
  • Truy cập: Database connections, API endpoints
  • Cập nhật: Daily synchronization
  • Metadata: Law codes, effective dates, amendments

3. Websites chính thức

  • Nguồn: Cổng thông tin tỉnh, bộ ngành
  • Kỹ thuật: Web scraping, RSS feeds
  • Tần suất: Scheduled crawls
  • Xử lý: HTML parsing, content extraction

4. External APIs

  • Nguồn: Các hệ thống bên thứ ba
  • Authentication: OAuth, API keys
  • Rate limiting: Respect API limits
  • Error handling: Retry mechanisms

🏗️ Kiến trúc Crawler 3 bước

🔧 Công nghệ và Tools

Core Frameworks

  • Scrapy: Web crawling framework
  • Selenium: Browser automation
  • BeautifulSoup: HTML parsing
  • Requests: HTTP client
  • SQLAlchemy: Database connections

Specialized Libraries

  • Playwright: Modern browser automation
  • Feedparser: RSS/Atom feed processing
  • PyPDF2: PDF text extraction
  • OpenPyXL: Excel file processing
  • Pandas: Data manipulation

Infrastructure

  • Docker: Containerization
  • Kubernetes: Orchestration
  • Redis: Caching và queuing
  • PostgreSQL: Metadata storage

🔄 Quy trình Crawler 3 bước

Bước 1: Thu thập dữ liệu thô

  • Mục tiêu: Lấy dữ liệu thô từ các nguồn I.Office và VB QPPL
  • Database đích: S-ERP-23-03-APPCODE-CR
  • Nội dung:
    • Dữ liệu văn bản từ I.Office (các sở ngành, UBND, HĐND, xã)
    • Văn bản QPPL từ hệ thống Trung ương
    • Metadata cơ bản (ngày ban hành, cơ quan ban hành, etc.)
  • Tần suất: Real-time cho I.Office, daily cho VB QPPL

Bước 2: Chuyển hóa và chuẩn hóa

  • Mục tiêu: Xử lý dữ liệu thô thành định dạng chuẩn
  • Database đích: S-ERP-23-03-APPCODE
  • Index Elasticsearch: s-erp-23-03-appcode
  • Xử lý:
    • Chuẩn hóa format văn bản
    • Trích xuất metadata đầy đủ
    • Loại bỏ duplicate
    • Tạo searchable index
  • Kết quả: Dữ liệu đã chuẩn hóa, sẵn sàng cho huấn luyện

Bước 3: Huấn luyện và vector hóa

  • Mục tiêu: Chuyển dữ liệu thành vector embeddings cho AI
  • Database đích: S-ERP-23-03-APPCODE-AI (PostgreSQL vector)
  • Quy trình:
    • Chunking văn bản
    • Tạo embeddings với mô hình AI
    • Lưu trữ vectors
    • Tạo index cho tìm kiếm ngữ nghĩa
  • Kết quả: Kho dữ liệu AI sẵn sàng cho khai thác

� Chiến lược thu thập

Incremental Crawling

  • Change detection: Timestamp, hash comparison
  • Delta updates: Only fetch changed content
  • Version control: Track document versions
  • Conflict resolution: Handle concurrent updates

Rate Limiting & Politeness

  • Request throttling: Respect source limits
  • Random delays: Avoid detection patterns
  • User agents: Rotate browser signatures
  • IP rotation: Use proxy pools if needed

Error Handling & Recovery

  • Retry logic: Exponential backoff
  • Circuit breakers: Fail fast on persistent errors
  • Dead letter queues: Handle failed tasks
  • Manual intervention: Alert system for critical failures

🔍 Data Validation

Schema Validation

  • JSON Schema: For API responses
  • XML Validation: For structured documents
  • Content checks: Required fields, data types
  • Business rules: Domain-specific validations

Quality Assurance

  • Completeness: Check for missing data
  • Accuracy: Cross-reference with known sources
  • Consistency: Validate relationships
  • Timeliness: Ensure data freshness

📈 Monitoring & Analytics

Performance Metrics

  • Throughput: Documents crawled per hour
  • Success rate: Percentage of successful crawls
  • Latency: Time to complete crawl cycles
  • Resource usage: CPU, memory, bandwidth

Crawl Analytics

  • Coverage: Percentage of sources crawled
  • Freshness: Age of crawled data
  • Error patterns: Common failure modes
  • Trend analysis: Data volume over time

🔒 Security Considerations

Authentication

  • API keys: Secure key management
  • OAuth flows: Token refresh handling
  • Certificate pinning: SSL validation
  • VPN connections: Secure access to internal systems

Data Protection

  • Encryption: Data in transit
  • Access controls: Least privilege principle
  • Audit logging: Track all access and changes
  • Compliance: Adhere to data protection regulations

🚀 Scaling & Optimization

Horizontal Scaling

  • Worker pools: Multiple crawler instances
  • Distributed crawling: Coordinate across nodes
  • Load balancing: Distribute work evenly

Performance Optimization

  • Concurrent requests: Async processing
  • Caching: Response caching
  • Compression: Reduce bandwidth usage
  • Connection pooling: Reuse connections

📋 Workflow Examples

  1. Schedule trigger at 2 AM
  2. Fetch updated documents from VB QPPL
  3. Validate and deduplicate
  4. Store in raw data lake
  5. Update metadata database
  6. Send completion notification

Real-time Office Document Sync

  1. Webhook trigger from i.Office
  2. Authenticate and authorize
  3. Download new documents
  4. Extract metadata
  5. Queue for processing
  6. Update indexes

Crawler đảm bảo nguồn dữ liệu luôn phong phú, cập nhật và sẵn sàng cho các giai đoạn xử lý tiếp theo trong pipeline.