2017-05-16 7 views
1

セットアップリンク項目は、私は住宅の広告をこすりscrapyを使用してscrapy

でのhrefを解析されています。

ad-overview pageで、私は個々の広告にリンクしている階層を持つリストを取得します。 forループによって、hrefは第2のパーサ関数に送られ、広告ごとの住宅特性が得られます。

def parse(self, response): 
     # for href in list with hrefs 
     for href in response.xpath(
       '//*[@id]/@href', 
       ).extract()[1:-1]: 
      yield scrapy.Request(response.urljoin(href), 
        callback=self.parse_ad) 

def parse_ad(self, response): 
# here follows code to obtain housing characteristics per ad 

    yield {'char1': char1, 
      'char2': char2,} 

これは問題なく動作します。


問題

のhref以外にも、私はまた、使用して広告-概要ページから郵便番号のリストを取得し、

response.xpath('//*[@id]/div[1]/div/div[1]/div[1]/div[2]/meta').extract() 

最終的に私は

、持っていると思います
yield {'char1': char1, 
      'char2': char2, 
      'postal code': postal_code} 

しかし、わかりません。

  1. メイクパイソンは両方hrefとそれに対応するpostal_code
  2. を選択parse_ad()私が行くにはどうすればよい

yield関数にオーバーpostal_codeを運びますか?

+0

1.あなたはfucntion 'parse'で抽出された' href'を、使用したいですか? 2.いくつかのdictを「返す」ために、ある関数に複数の 'yield'呼び出しがあるかもしれません。 –

答えて

2

metaを使用し、別のコールバックメソッドへのコールバックメソッドからのもの「を続けていく」ために:

def parse(self, response): 
    for search_result in response.css(".room-tile.rowSearchResultRoom"): 
     postal_code = search_result.css("meta[itemprop=postalCode]::attr(content)").extract_first() 
     href = search_result.xpath("@href").extract_first() 

     yield scrapy.Request(response.urljoin(href), 
          meta={'postal_code': postal_code}, 
          callback=self.parse_ad) 

def parse_ad(self, response): 
    postal_code = response.meta['postal_code'] 

    # get char1 and char2.. 

    yield {'char1': char1, 
      'char2': char2, 
      'postal_code': postal_code} 
+0

うんざり、ありがとう! :D – LucSpan

関連する問題