2016-05-16 63 views
0

我有一個scrapy蜘蛛,爲每個物品抓取兩個數量。問題是我必須使用float方法,所以當發生其中一個被抓取的字段爲空時,我得到一個錯誤,並且蜘蛛停止抓取該頁面中的元素,並直接進入下一頁。Scrapy,錯誤後繼續爬行

有沒有可能告訴scrapy在錯誤後繼續爬行?這是我的蜘蛛的代碼。謝謝!

def parse(self, response): 
    for sel in response.xpath('//li[@class="oneclass"]'): 
     item = exampleItem() 
     item['quant1'] = float(sel.xpath('a/div/span[@class="exampleclass"]/span[@class="amount"]/text()')) 
     item['quant2'] = float(sel.xpath('div[@class="otherexampleclass"]/input/@max')) 
     yield item 

回答

3

/你可以把它包在一個try except塊:

def parse(self, response): 
    for sel in response.xpath('//li[@class="oneclass"]'): 
     try: 
      item = exampleItem() 
      item['quant1'] = float(sel.xpath('a/div/span[@class="exampleclass"]/span[@class="amount"]/text()')) 
      item['quant2'] = float(sel.xpath('div[@class="otherexampleclass"]/input/@max')) 
      yield item 
     except: 
      print "could not crawl {}".format(sel) 
+0

完美,這就是我一直在尋找。我將其標記爲正確。 – Joe82