Skip to main content

Rabbitmq for Distributed scraping

Project description

Scrapy分布式RabbitMQ调度器

安装

使用pip安装

pip install scrapy-rabbitmq-task

或者克隆这个项目并且执行setup.py安装

python setup.py install

使用

第一步: 在你的项目中的settings.py中添加配置项

# 指定项目的调度器
SCHEDULER = "scrapy_rabbitmq_scheduler.scheduler.SaaS"

# 指定rabbitmq的连接DSN
RABBITMQ_CONNECTION_PARAMETERS = 'amqp://guest:guest@localhost:5672/'

# 指定重试的http状态码(重新加回队列重试)
SCHEDULER_REQUEUE_ON_STATUS = [500]

# 指定下载器中间件, 确认任务是否成功
DOWNLOADER_MIDDLEWARES = {
    'scrapy_rabbitmq_scheduler.middleware.RabbitMQMiddleware': 999
}
# 指定item处理方式, item会加入到rabbitmq中
ITEM_PIPELINES = {
    'scrapy_rabbitmq_scheduler.pipelines.RabbitmqPipeline': 300,
}

第二步: 修改Spider的继承类

import scrapy
from scrapy_rabbitmq_scheduler.spiders import RabbitSpider

class CustomSpider(RabbitSpider):
    name = 'custom_spider'    
    queue_name = 'test_urls' # 指定任务队列的名称
    items_key = 'test_item' # 指定item队列名称

    def parse(self, response):
        item = ... # parse item
        yield item

第三步: 将任务写入到RabbitMQ队列

#!/usr/bin/env python
import pika
import settings

connection = pika.BlockingConnection(pika.URLParameters(settings.RABBITMQ_CONNECTION_PARAMETERS))
channel = connection.channel()

queue_key = 'test_urls'

# 读取文件中的链接并写入到队列中
with open('urls.txt') as f:
    for url in f:
        url = url.strip(' \n\r')
        channel.basic_publish(exchange='',
                        routing_key=queue_key,
                        body=url,
                        properties=pika.BasicProperties(
                            content_type='text/plain',
                            delivery_mode=2
                        ))

connection.close()

urls.txt

http://www.baidu.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrapy_rabbitmq_task-1.0.2.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scrapy_rabbitmq_task-1.0.2-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file scrapy_rabbitmq_task-1.0.2.tar.gz.

File metadata

  • Download URL: scrapy_rabbitmq_task-1.0.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.5

File hashes

Hashes for scrapy_rabbitmq_task-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7a99e767bcef5166ae0091ac5daf247aed7d396b79e7338c1b36323396eadeba
MD5 eb7c645e982122e8359ee9232d32dcd4
BLAKE2b-256 96d3521c8b8f64abf3bb1e80b1855c0a282a53d00b15043195f60bb60c7cf586

See more details on using hashes here.

File details

Details for the file scrapy_rabbitmq_task-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for scrapy_rabbitmq_task-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8af96e01cf2604461194a649a43cbc4d45873f0d0c8acefaf073fb58c6eba177
MD5 396065bfb4882007de9272236be54be5
BLAKE2b-256 ceeaf60a9da32bb3c2fcf1f5e67b5c5b952220acb64c1114ff3c3a3bd1ec6c6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page