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
|
import pymysql
from twisted.enterprise import adbapi
from pymysql import cursors
# 连接池方式保存数据库
class JianshuTwistedPipeline(object):
def __init__(self):
dbparams = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': '',
'database': 'python',
'charset': 'utf8',
'cursorclass': cursors.DictCursor
}
# 建立连接池
self.dbpool = adbapi.ConnectionPool('pymysql', **dbparams)
# 插入数据
def insert_item(self, cursor, item):
sql = """INSERT INTO jianshu(article_id,article_title,article_content,origin_url) VALUES (%s, %s, %s, %s)"""
# 游标执行sql
cursor.execute(sql, (item['article_id'],item['article_title'],item['article_content'], item['origin_url']))
# 异常处理
def handle_error(self, error, item, spider):
print('**********error**********')
print(error)
# 执行
def process_item(self, item, spider):
# 运行
defer = self.dbpool.runInteraction(self.insert_item, item)
# 增加异常处理函数
defer.addErrback(self.handle_error, item, spider)
return item
|