scrapyd
repository·master·Indexed 25 days ago
https://github.com/scrapy/scrapydA deployment service for Scrapy spiders (version 1.6.0) that allows users to upload Scrapy projects and control spider execution via a JSON-based HTTP API. It includes a CLI for deployment, a hierarchical configuration system, and support for managing project versions, scheduling crawl jobs, and monitoring job status.
What's inside scrapyd
- Scrapyd is a service designed for deploying and running Scrapy spiders. It provides a mechanism to upload Scrapy projects and manage their execution via a JSON API.
Manage Scrapy projects and versions in Scrapyd
masterScrapyd allows you to manage multiple Scrapy projects, where each project can contain multiple versions. When you schedule a spider to crawl, Scrapyd uses the latest version by default.
Version Selection Logic:
- If version names are standard Python version specifiers (e.g.,
1.0,1.0rc1), they are sorted according to Python versioning rules. - Otherwise, the latest version is determined by alphabetical order (the alphabetically greatest name).
- If version names are standard Python version specifiers (e.g.,
Understand how Scrapyd operates
masterScrapyd operates as a server (typically a daemon) that handles requests via an API and a web interface. Its primary functions are uploading projects and scheduling crawls.
When a crawl is scheduled, Scrapyd spawns a process that executes the equivalent of:
scrapy crawl myspiderScrapyd manages multiple processes in parallel and controls the number of concurrent processes. For advanced configuration of the process launcher, refer to the
config-launcherdocumentation.Use the Scrapyd CLI
masterScrapyd is a command-line application used for deploying and running Scrapy spiders. It acts as a wrapper aroundtwistd. When running Scrapyd, the--nodaemonoption is always enabled by default.Add Custom Webservices to Scrapyd
masterYou can add new API endpoints by defining them in the
[services]section of your configuration file. Map a JSON filename to a Python module/class.Example configuration:
[services] mywebservice.json = amodule.anothermodule.MyWebServiceTo remove a default webservice, set its name to an empty string:
[services] daemonstatus.json =[services] mywebservice.json = amodule.anothermodule.MyWebServiceStart the Scrapyd service
masterRun thescrapydcommand to start the service locally.scrapydUpload a project to Scrapyd
masterTo upload a project, you must build a Python egg and upload it via theaddversion.jsonwebservice. The easiest way to do this is using thescrapyd-deploycommand from thescrapyd-clientpackage. Once configured, run:scrapyd-deployCreate a Docker image for Scrapyd and Scrapy projects
masterYou can containerize the Scrapyd service and your Scrapy projects using a multi-stage Dockerfile. The process involves a
build-stageto create the project's.eggfile usingscrapyd-deploy, and a final stage based onpython:alpinethat installsscrapydand the necessary system dependencies (likelibxml2andlibxslt) to run the service.Key configuration steps in the Dockerfile:
- Volumes: Mount
/etc/scrapyd/for configuration and/var/lib/scrapyd/for runtime data. - Ports: The service exposes port
6800by default. - Entrypoint: Uses
scrapydwith the--pidfile=flag.
# Build an egg of your project. FROM python as build-stage RUN pip install --no-cache-dir scrapyd-client WORKDIR /workdir COPY . . RUN scrapyd-deploy --build-egg=myproject.egg # Build the image. FROM python:alpine # Install Scrapy dependencies - and any others for your project. RUN apk --no-cache add --virtual build-dependencies \ gcc \ musl-dev \ libffi-dev \ libressl-dev \ libxml2-dev \ libxslt-dev \ \ && pip install --no-cache-dir \ scrapyd \ && apk del build-dependencies \ && apk add \ libressl \ libxml2 \ libxslt # Mount two volumes for configuration and runtime. VOLUME /etc/scrapyd/ /var/lib/scrapyd/ COPY ./scrapyd.conf /etc/scrapyd/ RUN mkdir -p /src/eggs/myproject COPY --from=build-stage /workdir/myproject.egg /src/eggs/myproject/1.egg EXPOSE 6800 ENTRYPOINT ["scrapyd", "--pidfile="]- Volumes: Mount
Access the Scrapyd web interface
masterScrapyd provides a minimal built-in web interface for monitoring running processes, accessing log files, and viewing item feeds. By default, the interface is available at
http://localhost:6800/.If you require more advanced management features, you can use third-party tools such as:
ScrapydWebspider-admin-pro
Run Scrapyd within a Scrapy project
masterYou can run thescrapydcommand directly from a directory that contains ascrapy.cfgfile, or from any directory that has ascrapy.cfgfile in one of its parent directories. This allows Scrapyd to use the settings defined in your Scrapy project.Configure Scrapyd via configuration files
masterScrapyd uses a hierarchical configuration system. It always loads a default configuration file, which can be overridden by other files. When multiple configuration files are present, values in later files take priority according to the following order:
c:\scrapyd\scrapyd.conf(Windows)/etc/scrapyd/scrapyd.conf(Unix)/etc/scrapyd/conf.d/*(Unix, in alphabetical order)scrapyd.confin the current directory~/.scrapyd.confin the user's home directory- The closest
scrapy.cfgfile (starting in the current directory and traversing upward)
Install Scrapyd
masterInstall the Scrapyd service using pip.
pip install scrapyd