-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelines.py
51 lines (38 loc) · 1.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
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from weibo.spiders.wb import WbSpider
from itemadapter import ItemAdapter
import csv
"""
class WeiboPipeline:
def __init__(self):
self.file = None
self.writer = None
@classmethod
def from_crawler(cls, crawler):
return cls()
def open_spider(self, spider):
self.file = open(f'{spider.Q}.csv', 'w', encoding='utf8', newline='')
fieldnames = ['_id', '_name', '_text']
self.writer = csv.DictWriter(self.file, fieldnames=fieldnames)
self.writer.writeheader()
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
self.writer.writerow(dict(item))
return item
"""
class WeiboPipeline:
def __init__(self):
self.f = open(f'{WbSpider.Q}.csv', 'w', encoding='utf8', newline='')
self.file_name = ['_id', '_name', '_text']
self.writer = csv.DictWriter(self.f, fieldnames=self.file_name)
self.writer.writeheader()
def process_item(self, item, spider):
self.writer.writerow(dict(item))
return item
def close_spider(self, spider):
self.f.close()