直接上例子代码:
# coding: utf8
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from werkzeug.utils import import_string, find_modules
scope = 'all'
process = CrawlerProcess(settings=get_project_settings())
for module_string in find_modules('CheckSpider.spiders'):
module = import_string(module_string)
class_string = module_string.split('.')[-1].capitalize() + 'Spider'
spider_class = getattr(module, class_string)
process.crawl(spider_class, scope)
process.start()
这是我在工作中的一个 用例, 总共有十个爬虫,同时启动十个爬虫。
利用werkzeug 库来实现批量导入所对应的spidercls(爬虫对应的类),初始化CrawlerProcess需要将setting对象传入,通过get_project_settings获取setting配置。
欢迎关注公众号:日常bug,每天写至少一篇技术文章,每天进步一点点。