2016-12-14 10 views
0

私はドキュメントを読んで、コマンドラインがこのようになっていることを確認します。 scrapy runspider getspecificimg.py -a ip='lizhe'パラメータをscrapyに渡す-aオプションの使い方は?

そして、私のクモのコードは次のようである:

class GetImage(scrapy.Spider): 
    name = 'ImageSpider' 
    start_urls = ['https://www.pexels.com/'] 

# Get the input argument 
    # NameNeedSearch = InputPara 
    NameNeedSearch = ip 

しかし、私が得る結果はつまりip isn't defined why? 20161211162649.bmp

- アップデート - 私は、変数を渡したいですそれを使用してfull urlを連結し、それをstart_url として使用してください。私のコードは次のようなものです:self is not definedなぜですか?

class GetImage(scrapy.Spider): 
    name = 'ImageSpider' 
# Get the input argument 
    NameNeedSearch = self.ip 
    # startUrl = 'https://www.pexels.com/' + 
    start_urls = ['https://www.pexels.com/'] 

答えて

1

あなたは一例__init__またはクロールを開始するときに呼び出されstart_requestsのために、あなたのGetImageクラスのいずれかの方法でselfを使用してコードを記述する必要があります。

フレームワークによって呼び出された場合、これらの方法は、最初の引数として取得するメソッドのシグネチャで使用される従来のself変数として使用可能なクラスインスタンス自体は、(それだけ条約の)

class GetImage(scrapy.Spider): 
    name = 'ImageSpider' 
    start_urls = ['https://www.pexels.com/'] 

    def start_requests(self): 
     # self points to the spider instance 
     # that was initialized by the scrapy framework when starting a crawl 
     # 
     # spider instances are "augmented" with crawl arguments 
     # available as instance attributes, 
     # self.ip has the (string) value passed on the command line 
     # with `-a ip=somevalue` 
     for url in self.start_urls: 
      yield scrapy.Request(url+self.ip, dont_filter=True) 
関連する問題