-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelines.py
96 lines (81 loc) · 3.61 KB
/
pipelines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import codecs
import json
'''
class ScrapySpiderPipeline(object):
# pass
# 自定义json文件导出
def __init__(self):
self.name = None
self.file = None
# filename = 'lingshou' # change filename according to the name of website
# self.name = filename
# self.file = codecs.open('./json_files/{}.json'.format(self.name), 'w', encoding="utf-8")
#
def process_item(self, item, spider):
self.name = spider.name
# self.file = codecs.open('./json_files/{}.json'.format(self.name), 'a', encoding="utf-8")
self.file = codecs.open('/home/sen/Workspace/handle_data/lab_scrapy/spider_crawl_start_55/scrapy_spider/scrapy_spider/json_files/{}.json'.format(self.name), 'a', encoding="utf-8")
lines = json.dumps(dict(item), ensure_ascii=False) + "," + "\n"
self.file.write(lines)
return item
def close_spider(self, spider):
self.file.close()
self.convert_mult_objects_to_list()
def convert_mult_objects_to_list(self):
import os
with open('/home/sen/Workspace/handle_data/lab_scrapy/spider_crawl_start_55/scrapy_spider/scrapy_spider/json_files/{}.json'.format(self.name), "rb+") as json_file:
json_file.seek(-2, os.SEEK_END)
json_file.truncate()
with open('/home/sen/Workspace/handle_data/lab_scrapy/spider_crawl_start_55/scrapy_spider/scrapy_spider/json_files/{}.json'.format(self.name), "a", encoding="utf-8") as json_file:
json_file.write("]")
with open('/home/sen/Workspace/handle_data/lab_scrapy/spider_crawl_start_55/scrapy_spider/scrapy_spider/json_files/{}.json'.format(self.name), "r+", encoding="utf-8") as f:
content = f.read()
f.seek(0)
f.write('[' + content)
#
# def __init__(self):
# self.file = codecs.open('xingtang.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 close_spider(self, spider):
# self.file.close()
# self.convert_mult_objects_to_list()
#
# def convert_mult_objects_to_list(self):
# import os
# with open('xingtang.json', "rb+") as json_file:
# json_file.seek(-2, os.SEEK_END)
# json_file.truncate()
# with open('xingtang.json', "a") as json_file:
# json_file.write("]")
# with open('xingtang.json', "r+") as f:
# content = f.read()
# f.seek(0)
# f.write('[' + content)
'''
import json
from itemadapter import ItemAdapter
class JsonWriterPipeline:
def open_spider(self, spider):
# self.file = open('items.jl', 'w')
self.name = spider.name
# self.file = codecs.open('./json_files/{}.json'.format(self.name), 'a', encoding="utf-8")
self.file = codecs.open('/home/sen/Workspace/handle_data/lab_scrapy/spider_crawl_start_55/scrapy_spider/scrapy_spider/json_files/{}.json'.format(self.name), 'a', encoding="utf-8")
# lines = json.dumps(dict(item), ensure_ascii=False) + "," + "\n"
# self.file.write(lines)
# return item
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
line = json.dumps(ItemAdapter(item).asdict(), ensure_ascii=False) + "," + "\n"
self.file.write(line)
return item