-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelines.py
122 lines (85 loc) · 3.41 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
#
#class TutorialPipeline(object):
# def process_item(self, item, spider):
# return item
#Setting pipeline to remove unicode characters & opening file with same name as that of spider
#from scrapy.exporters import JsonItemExporter
#from spiders.quotes2_spider import Quotes2Spider as spider
#class JsonWithEncodingPipeline(object):
# def __init__(self):
# self.file = open( spider.name + '.jl', '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
# setting pipeline to remove unicode characters with codecs & opening & writing to a file automatically
#import json
#import codecs
#from spiders.quotes2_spider import Quotes2Spider as spider
#class JsonWithEncodingPipeline(object):
# def __init__(self):
# self.file = codecs.open('quotes2.json', 'w', encoding='utf-8')
# def process_item(self, item, spider):
# line = json.dumps(dict(item), ensure_ascii=False) + "\n"
# self.file.write(line)
# return item
# def spider_closed(self, spider):
# self.file.close()
#from __future__ import unicode_literals
#import json
#import codecs
#from spiders.quotes2_spider import Quotes2Spider as spider
#from scrapy.contrib.loader.processor import MapCompose
#class JsonWithEncodingPipeline(object):
# def __init__(self):
# self.file = codecs.open( spider.name +'.json', 'w', encoding='utf-8')
# def process_item(self, item, spider):
# print(json.dumps((item['text']), ensure_ascii=False) + "\n")
# print("*********{}****".format(json.dumps((item['text']), ensure_ascii = False )+ "\n"))
# print("@@@@@@@@@@@@{}@@@@@@@@@@".format(json.dumps((item['author']), ensure_ascii = False )+ "\n"))
# print("#############{}######".format(json.dumps((item['tag']), ensure_ascii = False )+ "\n"))
# text1 = json.dumps((item['text']), ensure_ascii = False )
# auth = json.dumps((item['author']) )
# tag = json.dumps((item['tag']), ensure_ascii = False )+ "\n"
# line= (text1 + auth + tag)
# print(line)
# self.file.write(line)
#return item
# def spider_closed(self, spider):
# self.file.close()
#
#from __future__ import unicode_literals
#import json
#from spiders.quotes2_spider import Quotes2Spider as spider
#from scrapy.exporters import JsonLinesItemExporter
#class MyJsonLinesItemExporter(JsonLinesItemExporter):
#class JsonWithEncodingPipeline(object):
# def __init__(self):
# self.file = (self).__init__(file)
# def process_item(self, item, spider, **kwargs):
# if item:
#return item
# if item['text']:
# text1 = json.dumps((item['text']), ensure_ascii = False )
# print("&&&&&&&&&&{}&&&&&&&&&".format(text1))
# line = text1
# self.file.write(line)
# return item
# line = json.dumps(dict(item), ensure_ascii=False) + "\n"
#print str(item['text'][0]).encode("utf-8")
# item['text'] = str(item['text'][0]).encode("utf-8")
# item['author'] = str(item['author'][0]).encode("utf-8")
# item['tag'] = str(item['tag']).encode("utf-8")
# print (item['text'])
# print (item['author'])
# print (item['tag'])
#return item