2014-04-29 6 views
4

나는 내가 SgmlLinkExtractor의 수 속성이 명령 줄 매개 변수에 지정된 것을 원하는 다음scrapy에서 crawlspider의 명령 줄 매개 변수에 액세스하는 방법은 무엇입니까?

name = 'example.com' 
allowed_domains = ['example.com'] 
start_urls = ['http://www.example.com'] 

rules = (
    # Extract links matching 'category.php' (but not matching 'subsection.php') 
    # and follow links from them (since no callback means follow=True by default). 
    Rule(SgmlLinkExtractor(allow=('category\.php',), deny=('subsection\.php',))), 

    # Extract links matching 'item.php' and parse them with the spider's method parse_item 
    Rule(SgmlLinkExtractor(allow=('item\.php',)), callback='parse_item'), 
) 

처럼 확장 CrawlSpider의 규칙 정의에 사용되는 scrapy crawl ... 명령 줄에서 매개 변수를 전달하려면 . 내가 봤 거든 스파이더의 __init__ 메서드에서 매개 변수 값을 얻을 수 있지만 어떻게 명령 줄에서 매개 변수를 얻을 수있는 규칙 정의에 사용되는 발견?

답변

5

당신은 __init__ 방법에 거미의 rules 속성을 구축 할 수 있습니다, 뭔가 같은 :

scrapy crawl example.com -a allow="item\.php" 
:이 같은 명령 행에 allow 속성을 전달

class MySpider(CrawlSpider): 

    name = 'example.com' 
    allowed_domains = ['example.com'] 
    start_urls = ['http://www.example.com'] 

    def __init__(self, allow=None, *args, **kwargs): 
     self.rules = (
      Rule(SgmlLinkExtractor(allow=(self.allow,),)), 
     ) 
     super(MySpider, self).__init__(*args, **kwargs) 

그리고

관련 문제