2016-06-28 31 views
0

這裏我的代碼,我提出:我想蟒蛇pyspider腳本店輸出到CSV或JSON

import json 
from pyspider.libs.base_handler import * 

f = open("demo.txt","w") 
class Handler(BaseHandler): 
    crawl_config = { 
    } 

    @every(minutes=0,seconds = 0) 
    def on_start(self): 
     self.crawl('Any URL', callback=self.index_page) 

    @config(priority=2) 
    def detail_page(self, response): 
     img = "" 
     for each in response.doc("img[id='landingImage']").items(): 
      img = each 
     f.write("\n"+response.doc('title').text() + "\t" + response.doc("span[id^='priceblock']").text()) 
     return { 
      "url": response.url, 
      "title": response.doc('title').text(), 
      "img_url":img.attr['src'], 
      "Price":response.doc("span[id^='priceblock']").text(), 
      "Availability":response.doc("div[id='availability']").text() 
     } 

在上面的代碼中,我從一個鏈接廢料的數據,但我不能在JSON或CSV格式

獲取輸出

enter image description here

回答

0

您可以從WebUI下載數據,或通過覆蓋on_result方法追加數據到文件中。