简体   繁体   中英

Is there any way to change scrapy spider's name by script

I did a scrapy-redis crawler and decide to do a distributed crawler. For the more, I want to make it to a tasks-based, one task one name. So, I plan to change my spider's name to task's name and distinguish each task with this name. Because of that , I met a question how to change the name of spider during I run the web management.

This is my code, and it is in immature:

#-*- encoding: utf-8 -*-
import redis
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from scrapy_redis.spiders import RedisSpider
import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')
db_name = 'news'
db = client[db_name]

class NewsSpider(RedisSpider):
    """Spider that reads urls from redis queue (myspider:start_urls)."""
    name = 'news'
    redis_key = 'news:start_urls'
    start_urls = ["http://www.bbc.com/news"]

    def parse(self, response):
        pass
    # I add those  ,setname and getname
    def setname(self, name):
        self.name = name

    def getname(self):
        return self.name

def start():
    news_spider = NewsSpider()
    news_spider.setname('test_spider_name')
    print news_spider.getname()
    r = redis.Redis(host='127.0.0.1', port=6379, db=0)
    r.lpush('news:start_urls', 'http://news.sohu.com/')
    process = CrawlerProcess(get_project_settings())
    process.crawl('test_spider_name')
    process.start()  # the script will block here until the crawling is finished

if __name__ == '__main__':
    start()

And there is the error:

test_spider_name
2017-05-26 20:14:05 [scrapy.utils.log] INFO: Scrapy 1.3.3 started (bot: scrapybot)
2017-05-26 20:14:05 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'geospider.spiders', 'SPIDER_MODULES': ['geospider.spiders'], 'COOKIES_ENABLED': False, 'SCHEDULER': 'scrapy_redis.scheduler.Scheduler', 'DUPEFILTER_CLASS': 'scrapy_redis.dupefilter.RFPDupeFilter'}
Traceback (most recent call last):
  File "/home/kui/work/python/project/bigcrawler/geospider/control/command.py", line 29, in <module>
    start()
  File "/home/kui/work/python/project/bigcrawler/geospider/control/command.py", line 23, in start
    process.crawl('test_spider_name')
  File "/home/kui/work/python/env/lib/python2.7/site-packages/scrapy/crawler.py", line 162, in crawl
    crawler = self.create_crawler(crawler_or_spidercls)
  File "/home/kui/work/python/env/lib/python2.7/site-packages/scrapy/crawler.py", line 190, in create_crawler
    return self._create_crawler(crawler_or_spidercls)
  File "/home/kui/work/python/env/lib/python2.7/site-packages/scrapy/crawler.py", line 194, in _create_crawler
    spidercls = self.spider_loader.load(spidercls)
  File "/home/kui/work/python/env/lib/python2.7/site-packages/scrapy/spiderloader.py", line 55, in load
    raise KeyError("Spider not found: {}".format(spider_name))
KeyError: 'Spider not found: test_spider_name'

I know it's a fool way and I am searching for a long time on net.But no use. Please help me or give some ideas how to achieve this.

Thanks in advance.

This might help:

class NewsSpider(RedisSpider):
    """Spider that reads urls from redis queue (myspider:start_urls)."""
    name = 'news_redis'
    redis_key = 'news:start_urls'
    start_urls = ["http://www.bbc.com/news"]

    def parse(self, response):
        pass

def start():
    news_spider = NewsSpider()

    # Set name & redis_key for NewsSpider
    NewsSpider.name = 'test_spider_name_redis'
    NewsSpider.redis_key = NewsSpider.name + ':start_urls'

    r = redis.Redis(host='127.0.0.1', port=6379, db=0)
    r.lpush(NewsSpider.name + ':start_urls', 'http://news.sohu.com/')
    process = CrawlerProcess(get_project_settings())
    process.crawl(NewsSpider)
    process.start()  # the script will block here until the crawling is finished

if __name__ == '__main__':
    start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM