2017-09-16 5 views
0

関数から値を渡そうとしています。複数のサイトから値を取得する

私はドキュメントを調べて、それを理解できませんでした。 REF:ここ

def parse_page1(self, response): 
    item = MyItem() 
    item['main_url'] = response.url 
    request = scrapy.Request("http://www.example.com/some_page.html", 
          callback=self.parse_page2) 
    request.meta['item'] = item 
    yield request 

def parse_page2(self, response): 
    item = response.meta['item'] 
    item['other_url'] = response.url 
    yield item 

は私がachiveしたいのpsudoコードです:

import scrapy 

class GotoSpider(scrapy.Spider): 
    name = 'goto' 
    allowed_domains = ['first.com', 'second.com] 
    start_urls = ['http://first.com/'] 

def parse(self, response): 
    name = response.xpath(...) 
    price = scrapy.Request(second.com, callback = self.parse_check) 
    yield(name, price) 


def parse_check(self, response): 
    price = response.xpath(...) 
    return price 
+0

両方のサイトからの情報を含む1つのアイテムが必要ですか?サイトごとに1つのアイテムが必要ですか? – eLRuLL

+0

いいえ、私はすべての変数を含むオブジェクトを望んでいません、私は異なるvarsが欲しいです。それが不可能で、私がしなければならない場合は、1つのオブジェクト。 – daniel

答えて

0

これは、あなたが他の方法に任意の値、リンクなどを渡すことができる方法です。

import scrapy 

class GotoSpider(scrapy.Spider): 
    name = 'goto' 
    allowed_domains = ['first.com', 'second.com'] 
    start_urls = ['http://first.com/'] 

    def parse(self, response): 
     name = response.xpath(...) 
     link = response.xpath(...) # link for second.com where you may find the price 
     request = scrapy.Request(url=link, callback = self.parse_check) 
     request.meta['name'] = name 
     yield request 

    def parse_check(self, response): 
     name = response.meta['name'] 
     price = response.xpath(...) 
     yield {"name":name,"price":price} #Assuming that in your "items.py" the fields are declared as name, price 
+0

大変ありがとうございます。ついに良い簡単な答え!私は他のstackoverflowの質問を見ていただけで、それを理解することはできませんでした。しかし今はそのクリスタルクリア。ありがとうございました! – daniel

+0

btwあなたのソリューションは関数に値を渡していましたが、どうすれば別の方法で行くことができますか?名前を送信する代わりに、価格を受け取る。 – daniel

関連する問題