2017-05-07 5 views
2

ウェブサイトの詳細ページをリスティングページから拝見していますが、詳細ページごとに違いがあります。リストから異なる詳細ページを解析する治療法

第一詳細ページ:

<div class="td-post-content"> 
    <p style="text-align: justify;"> 
     <strong>[ Karda Natam ]</strong> 
     <br> 
     <strong>ITANAGAR, May 6:</strong> Nacho, Taksing, Siyum and ... 
     <br> “Offices are without ... 
    </p> 
</div> 

第二詳細ページ:

<div class="td-post-content"> 
    <p style="text-align: justify;"> 
     <strong>Guwahati, May 6 (PTI)</strong> Sarbananda Sonowal today ... 
     <br> “Books are a potent tool to create ... 
    </p> 
</div> 

第三詳細ページ:

<div class="td-post-content"> 
    <h3 style="text-align: justify;"><strong>Flights Of Fantasy</strong></h3> 
    <p style="text-align: justify;"> 
     <strong>[ M Panging ]</strong> 
     <br> This state of denial ... 
    </p> 
</div> 

私はディテールから、著者およびポスト日付を解析しようとしていますページ:

class ArunachaltimesSpider(scrapy.Spider): 
    ... 
    ... 

    def parse(self, response): 
     urls = response.css("div.td-ss-main-content > div.td_module_16 > div.item-details > h3.entry-title > a::attr(href)").extract() 
     for url in urls: 
      yield scrapy.Request(url=url, callback=self.parse_detail) 

     next = response.xpath("// ...')]/@href").extract_first() 
     if next: 
      yield scrapy.Request(url=next, callback=self.parse) 

    def parse_detail(self, response): 
     strong_elements = response.css("div.td-ss-main-content").css("div.td-post-content").css("p > strong::text").extract() 
     for strong in strong_elements: 
      if ', ' in strong: 
       news_date = strong.split(', ')[1].replace(":", "") 
      elif '[ ' and ' ]' in strong: 
       author = strong 
      else: 
       news_date = None 
       author = None 
     yield { 
      'author': author, 
      'news_date': news_date 
     } 

しかし、私はこのエラーを取得しています:私はここで間違って

UnboundLocalError: local variable 'author' referenced before assignment

何をしているのですか?それぞれのページから著者とニュースの日付を取得する方法を教えてください。ありがとうございました。

答えて

0

strong_elementsはあなたのケースでは空の配列です。だからforループを実行しないでください。しかし、あなたはauthor forループを宣言しました。あなたのケースでは、宣言されていない(becoz forループは実行されていません)yieldでauthorを使用しています。上記のように最上位レベルの変数を設定してくださいforループ

関連する問題