本文整理汇总了Python中scrapy.exporters.JsonItemExporter类的典型用法代码示例。如果您正苦于以下问题:Python JsonItemExporter类的具体用法?Python JsonItemExporter怎么用?Python JsonItemExporter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JsonItemExporter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BaseFilePipeline
class BaseFilePipeline(object):
def __init__(self,saved_path):
self.files = {}
self.saved_path = saved_path
self.exporter = None
@classmethod
def from_crawler(cls, crawler):
pipeline = cls(crawler.settings.get('SAVED_PATH'))
return pipeline
def open_spider(self, spider):
tp = self.gettype()['name']
filename = '%s_%s.json' % (spider.name,tp)
filename = os.path.join(self.saved_path,filename)
file_ = open(filename,'w+b')
self.files[spider] = file_
self.exporter = JsonItemExporter(file_,ensure_ascii=False,encoding='utf-8')
self.exporter.start_exporting()
def gettype():
pass
def close_spider(self, spider):
self.exporter.finish_exporting()
file_ = self.files.pop(spider)
file_.close()
示例2: spider_opened
def spider_opened(self, spider):
if spider.__class__ == MayorsSpider:
mayor_file = open("data/mayor_candidates.json", "w+b")
council_file = open("data/city_counsils.json", "w+b")
self.files.append(mayor_file)
self.files.append(council_file)
self.mayor_exporter = JsonItemExporter(mayor_file)
self.council_exporter = JsonItemExporter(council_file)
self.mayor_exporter.start_exporting()
self.council_exporter.start_exporting()
elif spider.__class__ == RegionCountiesSpider:
counties_file = open("data/region_counties.json", "w+b")
self.counties_exporter = JsonItemExporter(counties_file)
self.files.append(counties_file)
示例3: open_spider
def open_spider(self, spider):
# Creates 4 files for storage scraped items
self.category_file = open('spider/scraped/category.json', 'wb')
self.category_exporter = JsonItemExporter(self.category_file, encoding="utf-8")
self.category_exporter.start_exporting()
self.product_file = open('spider/scraped/product.json', 'wb')
self.product_exporter = JsonItemExporter(self.product_file, encoding="utf-8")
self.product_exporter.start_exporting()
self.shop_file = open('spider/scraped/shop.json', 'wb')
self.shop_exporter = JsonItemExporter(self.shop_file, encoding="utf-8")
self.shop_exporter.start_exporting()
self.product_price_file = open('spider/scraped/productprice.json', 'wb')
self.product_price_exporter = JsonItemExporter(self.product_price_file, encoding="utf-8")
self.product_price_exporter.start_exporting()
示例4: __init__
def __init__(self, spider_name):
self.file = open("output/{}_recipes.json".format(spider_name), 'wb')
self.file.write(
'{"date_scraped": "%s", "recipes": ' % datetime.datetime.now()
)
self.exporter = JsonItemExporter(self.file, encoding='utf-8',
ensure_ascii=False)
self.exporter.start_exporting()
示例5: open_spider
def open_spider(self, spider):
tp = self.gettype()['name']
filename = '%s_%s.json' % (spider.name,tp)
filename = os.path.join(self.saved_path,filename)
file_ = open(filename,'w+b')
self.files[spider] = file_
self.exporter = JsonItemExporter(file_,ensure_ascii=False,encoding='utf-8')
self.exporter.start_exporting()
示例6: from_crawler
class JsonPipelineExporterMixin:
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
file = open('%s_items.json' % spider.name, 'w+b')
self.files[spider] = file
self.exporter = JsonItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
item = self.pre_process_item(item)
self.exporter.export_item(item)
return item
def pre_process_item(self, item):
return item
示例7: JsonPipeline
class JsonPipeline(object):
def __init__(self):
self.files = {}
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
file = open('/home/gaoliang/Desktop/result.json', 'w+b')
self.files[spider] = file
self.exporter = JsonItemExporter(file, ensure_ascii=False) # 添加ensure_ascii=False用于使json保存中文不乱码
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
示例8: SaveItemToJson
class SaveItemToJson(object):
def __init__(self):
self.files = {}
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
file = open('%s_items.json' % spider.name, 'w+b')
self.files[spider] = file
self.exporter = JsonItemExporter(file=file)
print self.exporter
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
示例9: JsonPipeline
class JsonPipeline(object):
"""Save Pipeline output to JSON."""
def __init__(self, spider_name):
self.file = open("output/{}_recipes.json".format(spider_name), 'wb')
self.file.write(
'{"date_scraped": "%s", "recipes": ' % datetime.datetime.now()
)
self.exporter = JsonItemExporter(self.file, encoding='utf-8',
ensure_ascii=False)
self.exporter.start_exporting()
@classmethod
def from_crawler(cls, crawler):
return cls(
spider_name=crawler.spider.name
)
def close_spider(self):
self.exporter.finish_exporting()
self.file.write("}")
self.file.close()
def process_item(self, item):
self.exporter.export_item(item)
return item
示例10: JsonExportPipeline
class JsonExportPipeline(object):
def __init__(self):
_log.info('JsonExportPipeline.init....')
self.files = {}
@classmethod
def from_crawler(cls, crawler):
_log.info('JsonExportPipeline.from_crawler....')
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
_log.info('JsonExportPipeline.spider_opened....')
file = open('%s.json' % spider.name, 'w+b')
self.files[spider] = file
self.exporter = JsonItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self, spider):
_log.info('JsonExportPipeline.spider_closed....')
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
_log.info('JsonExportPipeline.process_item....')
self.exporter.export_item(item)
return item
示例11: JsonExportPipeline
class JsonExportPipeline(object):
"""
app.pipelines.exporter_json.JsonExportPipeline
"""
def __init__(self):
self.files = {}
self.exporter = None
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
file_json = open('%s_items.json' % spider.name, 'w+b')
self.files[spider] = file_json
self.exporter = JsonItemExporter(file_json)
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file_json = self.files.pop(spider)
file_json.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
示例12: JsonExportPipeline
class JsonExportPipeline(object):
def __init__(self):
self.files = {}
@classmethod
def from_crawler(cls, crawler):
pipeline = cls()
crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
return pipeline
def spider_opened(self, spider):
file = codecs.open('%s_data.json' % spider.name, 'w+b', encoding='utf-8')
self.files[spider] = file
self.exporter = JsonItemExporter(file)
self.exporter.start_exporting()
def spider_closed(self, spider):
self.exporter.finish_exporting()
file = self.files.pop(spider)
file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
示例13: JsonExporterPipeline
class JsonExporterPipeline(object):
# 调用scrapy提供的json_export 导出json文件
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 close_spider(self,spider):
self.exporter.finish_exporting()
self.file.close()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
示例14: process_item
def process_item(self, item, spider):
designer_dir_name = skutils.escape_filename(item['name'])
designer_dir_path = os.path.join(GlobalState.data_dir, designer_dir_name)
file_path = os.path.join(designer_dir_path, designer_dir_name)
# write json file
with open('%s.json' % file_path, 'w+b') as f:
exporter = JsonItemExporter(f)
exporter.start_exporting()
exporter.export_item(item)
exporter.finish_exporting()
# write excel file
excelutils.write_designer_excel(item, file_path, designer_dir_name)
return item
示例15: VisionsJsonPipeline
class VisionsJsonPipeline(object):
def __init__(self):
self.exporter = None
def open_spider(self, spider):
self.exporter = JsonItemExporter(open('%s.json' %spider.name, 'wb'))
self.exporter.start_exporting()
def process_item(self, item, spider):
self.exporter.export_item(item)
return item
def close_spider(self, spider):
self.exporter.finish_exporting()