-
Notifications
You must be signed in to change notification settings - Fork 75
/
pipelines.py
57 lines (47 loc) · 1.63 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
# -*- coding: utf-8 -*-
import json
import codecs
from scrapy.exceptions import DropItem
from model.config import DBSession
from model.config import Redis
from model.article import Article
# 去重
class DuplicatesPipeline(object):
def process_item(self, item, spider):
if Redis.exists('url:%s' % item['url']):
raise DropItem("Duplicate item found: %s" % item)
else:
Redis.set('url:%s' % item['url'],1)
return item
# 存储到数据库
class DataBasePipeline(object):
def open_spider(self, spider):
self.session = DBSession()
def process_item(self, item, spider):
a = Article(title=item["title"].encode("utf-8"),
url=item["url"],
body=item["body"].encode("utf-8"),
publish_time=item["publish_time"].encode("utf-8"),
source_site=item["source_site"].encode("utf-8"))
self.session.add(a)
self.session.commit()
def close_spider(self,spider):
self.session.close()
# 存储到文件
class JsonWriterPipeline(object):
def __init__(self):
self.file = codecs.open('items.json', 'w', encoding='utf-8')
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line.decode('unicode_escape'))
return item
# 爬取指定条数 100条
class CountDropPipline(object):
def __init__(self):
self.count = 100
def process_item(self, item, spider):
if self.count == 0:
raise DropItem("Over item found: %s" % item)
else:
self.count -= 1
return item