1. 程式人生 > >Scrapy學習-6-JSON數據處理

Scrapy學習-6-JSON數據處理

codec code -s nbsp json __init__ 學習 內置 sed

使用json模塊處理JSON數據
class JsonwithEncodingPipeline(object):

    def __init__(self):
        self.file = codecs.open(article.json, w, encoding=utf-8)

    def process_item(self, item, spider):
        lines = json.dumps(dict(item), ensure_ascii=False) + \n
        self.file.write(lines)
        
return item def spider_closed(self, spider): self.file.close()

內置JSON處理對象JsonItemExporter的使用
class JsonExporterPipeline(object):

    def __init__(self):
        self.file = open(articleexport.json, wb)
        self.exporter = JsonItemExporter(self.file, encoding=utf-8, ensure_ascii=False)
        self.exporter.start_exporting()

    
def spider_closed(self, spider): self.exporter.finish_exporting() self.file.close() def process_item(self, item, spider): self.exporter.export_item(item) return item

Scrapy學習-6-JSON數據處理