# scrapypp **Repository Path**: data_miner_1/scrapypp ## Basic Information - **Project Name**: scrapypp - **Description**: 使用cpp 设计的scrapy - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-22 - **Last Updated**: 2026-09-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # scrapy_cpp_autothrottle Scrapy 同款 **AutoThrottle** 扩展的 C++ 爬虫框架(基于 libcurl multi)。 ## 算法(与 Scrapy 官方一致) 每个域名(download slot)收到响应后: ``` target_delay = latency / N # N = AUTOTHROTTLE_TARGET_CONCURRENCY new_delay = (old_delay + target_delay) / 2 # 平滑过渡 if status not 2xx: new_delay = max(new_delay, old_delay) # 错误不加速 new_delay = clamp(new_delay, DOWNLOAD_DELAY, AUTOTHROTTLE_MAX_DELAY) ``` - per-domain 独立调速(以域名为 slot key),对齐 `CONCURRENT_REQUESTS_PER_DOMAIN` 语义。 - 非 2xx 不降低延迟:避免"服务器开始报错 → 爬虫反而加速"的反模式。 ## 与 Scrapy settings 对照 | Scrapy | AutoThrottle::Settings | |---|---| | AUTOTHROTTLE_ENABLED | enabled | | AUTOTHROTTLE_START_DELAY | start_delay (默认 5.0s) | | AUTOTHROTTLE_MAX_DELAY | max_delay (默认 60.0s) | | AUTOTHROTTLE_TARGET_CONCURRENCY | target_concurrency (默认 1.0) | | DOWNLOAD_DELAY | min_delay (默认 1.0s) | | AUTOTHROTTLE_DEBUG | debug | ## 文件清单 ``` include/ autothrottle.h # AutoThrottle Middleware(核心) engine.h # Engine(注入前按 slot 延迟限速) downloader.h/.cpp # Scrapy 级异步 Downloader(RAII + Middleware 链) request.h # Request / Response scheduler.h # 去重优先级队列 spider.h # Spider 基类 middleware.h # Middleware 责任链 retry_middleware.h # 重试 Middleware example_spider.h # 示例 Spider(含 502 触发非 2xx 分支) src/ main.cpp # 启用 AutoThrottle 的入口 CMakeLists.txt README.md ``` ## 构建 需要 libcurl 开发头文件:`sudo apt install -y libcurl4-openssl-dev g++ cmake`。 ```bash cmake -S . -B build && cmake --build build -j ./build/scrapy_cpp ``` 请求超时和重试次数通过 `Request` 设置: ```cpp request->timeout = 15; // 单次请求总超时,单位秒 request->connect_timeout = 10; // 建立连接超时,单位秒 request->max_retries = 3; // 失败后的重试次数,0 表示不重试 ``` 普通请求按 URL 去重;重试请求和 AutoThrottle 暂缓请求会强制重新入队。 启用 AddressSanitizer 排查内存问题: ```bash g++ -std=c++17 -fsanitize=address,undefined -g \ src/main.cpp src/downloader.cpp -I include \ -lcurl -lpthread -o scrapy_cpp_asan ./scrapy_cpp_asan ``` ## 启用 debug 后运行日志示例 ``` [AutoThrottle] slot: httpbin.org delay=0.50 latency=0.38 status=200 [AutoThrottle] slot: httpbin.org delay=0.75 latency=0.62 status=502 ```