refactor: optimize docker image and implement lightweight fetching

This commit improves the overall efficiency and reliability of the scraper by:

- Optimizing the Dockerfile by reducing layers, using `--no-install-recommends`, and consolidating Playwright installation.
- Adding resource limits (CPU/Memory) to the docker-compose configuration.
- Refactoring `main.go` to remove unused Cloudflare client structures and increasing cache TTL.
- Implementing a `lightweight_fetch` mechanism in `scrapling_fetch.py` using `urllib` to attempt fast requests before falling back to the heavier Scrapling/Playwright engine.
- Adding Cloudflare challenge detection to the lightweight fetcher.
This commit is contained in:
Tomas Dvorak
2026-05-11 19:50:59 +02:00
parent a8a4e1acaf
commit aa47f4309f
4 changed files with 474 additions and 440 deletions
+20 -38
View File
@@ -14,12 +14,12 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o facr-scraper .
# Python stage for Scrapling
FROM python:3.11-slim AS python-builder
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Install system dependencies for Playwright
RUN apt-get update && apt-get install -y \
wget \
gnupg \
ca-certificates \
curl \
RUN apt-get update && apt-get install -y --no-install-recommends \
wget curl ca-certificates gnupg \
&& rm -rf /var/lib/apt/lists/*
# Create virtual environment and install Scrapling
@@ -28,44 +28,27 @@ ENV PATH="/opt/scrapling/bin:$PATH"
COPY requirements-scrapling.txt .
RUN pip install --no-cache-dir -r requirements-scrapling.txt
# Install Playwright browsers
RUN playwright install chromium
RUN playwright install-deps
# Install Playwright browsers with deps in one layer
RUN playwright install chromium --with-deps
# Fix Python symlinks
RUN ln -sf /usr/local/bin/python /opt/scrapling/bin/python
RUN ln -sf /usr/local/bin/python /opt/scrapling/bin/python3
RUN ln -sf /usr/local/bin/python /opt/scrapling/bin/python \
&& ln -sf /usr/local/bin/python /opt/scrapling/bin/python3
# Final stage
FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PATH="/opt/scrapling/bin:$PATH"
# Install runtime dependencies for both Go and Playwright
RUN apt-get update && apt-get install -y \
ca-certificates \
wget \
curl \
gnupg \
libglib2.0-0 \
libgobject-2.0-0 \
libnspr4 \
libnss3 \
libdbus-1-3 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libcups2 \
libexpat1 \
libxcb1 \
libxkbcommon0 \
libatspi2.0-0 \
libx11-6 \
libxcomposite1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxrandr2 \
libgbm1 \
libcairo2 \
libpango-1.0-0 \
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates wget curl \
libglib2.0-0 libgobject-2.0-0 libnspr4 libnss3 libdbus-1-3 \
libatk1.0-0 libatk-bridge2.0-0 libcups2 libexpat1 libxcb1 \
libxkbcommon0 libatspi2.0-0 libx11-6 libxcomposite1 libxdamage1 \
libxext6 libxfixes3 libxrandr2 libgbm1 libcairo2 libpango-1.0-0 \
libasound2 \
&& rm -rf /var/lib/apt/lists/*
@@ -80,13 +63,12 @@ COPY --from=python-builder /opt/scrapling /opt/scrapling
# Copy Playwright browser cache
COPY --from=python-builder /root/.cache/ms-playwright /home/scraper/.cache/ms-playwright
ENV PATH="/opt/scrapling/bin:$PATH"
# Copy scrapling script
COPY scripts/scrapling_fetch.py /opt/scrapling/scripts/scrapling_fetch.py
# Create cache directory for Playwright
RUN mkdir -p /home/scraper/.cache && chown -R scraper:scraper /home/scraper
RUN mkdir -p /home/scraper/.cache && chown -R scraper:scraper /home/scraper /opt/scrapling
USER scraper
WORKDIR /home/scraper