장고에서 Scrapy web crawling framework을 실행할 수 있기를 원합니다. Scrapy 자체는 명령을 실행하기 위해 명령 행 도구 scrapy
만을 제공합니다. 즉, 도구는 의도적으로 외부 프로그램에서 호출하도록 작성되지 않았습니다.Django 사용자 지정 관리 명령 실행 Scrapy : Scrapy 옵션 포함 방법?
사용자 Mikhail Korobov은 nice solution으로, 즉 Django 사용자 지정 관리 명령에서 Scrapy를 호출했습니다. 편의를 위해 다음과 같이 솔루션을 반복합니다.
# -*- coding: utf-8 -*-
# myapp/management/commands/scrapy.py
from __future__ import absolute_import
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def run_from_argv(self, argv):
self._argv = argv
return super(Command, self).run_from_argv(argv)
def handle(self, *args, **options):
from scrapy.cmdline import execute
execute(self._argv[1:])
scrapy crawl domain.com
이제 Django 프로젝트에서 python manage.py scrapy crawl domain.com
을 할 수 있습니다. 그러나 Scrapy 명령의 옵션은 전혀 구문 분석되지 않습니다.
Usage: manage.py scrapy [options]
manage.py: error: no such option: -o
그래서 제 질문은, 어떻게 Scrapy의 명령 줄 옵션을 채택 할 수있는 사용자 지정 관리 명령을 확장 할 수있다 : 나는 python manage.py scrapy crawl domain.com -o scraped_data.json -t json
을한다면, 나는 단지 다음과 같은 응답을 얻을?
불행히도 Django의 documentation of this part은 그다지 광범위하지 않습니다. 또한 파이썬의 optparse module에 대한 문서를 읽었지만 후에 나에게 분명하지 않았습니다. 이 점에서 누구든지 나를 도울 수 있습니까? 많은 감사드립니다!
하지만 우리가 크롤링 할 최상위 디렉토리에 있지 않아야합니까 ?? 어떻게 된거 야? @pemistahl – Nabin