2017-05-27 3 views
0

thisサイトから2000年の気象データを収集しようとしています。特定のリンクを介してテキストファイルをスクラップして出力ファイルに保存できません

クモのための私のコードは次のとおりです。

import scrapy 
    from scrapy.contrib.spiders import CrawlSpider, Rule 
    from scrapy.contrib.linkextractors import LinkExtractor 

    class weather(CrawlSpider): 
     name = 'data' 
     start_urls = [ 
     'https://www1.ncdc.noaa.gov/pub/data/uscrn/products/daily01/' 
     ] 
     custom_settings = { 
       'DEPTH_LIMIT': '2', 
     } 
     rules = (
     Rule(LinkExtractor(restrict_xpaths= 
     ('//table/tr[2]/td[1]/a',)),callback='parse_item',follow=True), 
     ) 
     def parse_item(self, response): 
      for b in response.xpath('//table') 

      yield scrapy.request('/tr[4]/td[2]/a/@href').extract() 
      yield scrapy.request('/tr[5]/td[2]/a/@href').extract() 

「歩留まり」とのパスが2つのテキストファイルへのリンクです、私はこれらのテキストファイルからデータをこすりしたい2つの異なるファイルに別々に保管してくださいしかし、私はどのように続行するのか分からない。

答えて

0

私はそれに慣れていないので、通常はCrawlSpiderを使用しませんが、別のxpath(好ましくは"/tr[4]/td[2]/a/@href"より具体的なもの)を作成してコールバック機能を提供できるようです。

しかし、代わりにCrawlSpiderSpiderを使用して、「典型的な」scrapyプロジェクトでは、あなたは単に抽出してデータベースに格納し処理するために、別のコールバック関数でRequestをもたらすであろう。例:

def parse_item(self, response): 
    for b in response.xpath('//table') 
     url = b.xpath('/tr[4]/td[2]/a/@href').extract_first() 
     yield scrapy.Request(url=url, callback=extract_and_store) 
     url = b.xpath('/tr[5]/td[2]/a/@href').extract_first() 
     yield scrapy.Request(url=url, callback=extract_and_store) 

def extract_and_store(self, response): 
    """ 
    Scrape data and store it separately 
    """ 
関連する問題