Twisted是用Python实现的基于事件驱动的网络引擎框架;html
Twisted支持许多常见的传输及应用层协议,包括TCP、UDP、SSL/TLS、HTTP、IMAP、SSH、IRC以及FTP。就像Python同样,Twisted也具备“内置池”(batteries-included)的特色。Twisted对于其支持的全部协议都带有客户端和服务器实现,同时附带有基于命令行的工具,使得配置和部署产品级的Twisted应用变得很是方便。mysql
官网地址:https://twistedmatrix.com/trac/sql
MYSQL_HOST = 'localhost'
的形式;MYSQL_HOST = 'localhost' MYSQL_USER = 'xkd' MYSQL_PASSWORD = '123456' MYSQL_DATABASE = 'item_database' MYSQL_PORT = 3306 MYSQL_OPTIONAL = dict( USE_UNICODE = True, CHARSET = 'utf8', )
from .settings import MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_OPTIONAL class MysqlPipeline: def __init__(self): self.conn = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE, use_unicode=MYSQL_OPTIONAL.get('USE_UNICODE'), charset=MYSQL_OPTIONAL.get('CHARSET')) self.cursor = self.conn.cursor() def process_item(self, item, spider): sql = 'insert into item(title, image_url, date, image_path, url, url_id)' \ 'values (%s, %s, %s, %s, %s, %s)' date = item['date'] self.cursor.execute(sql, args=(item['title'], item['image_url'], date, item['image_path'], item['url'], item['url_id'])) self.conn.commit() return item def spider_closed(self, spider): self.cursor.close() self.conn.close()
首先建立一个用于异步写入数据的AIOMysqlItemPipeline类,而后在这个类的初始化方法中建立一个pool链接池;数据库
而后在from_settings()方法
中获取settings文件中的数据库配置信息,并将配置信息存入一个字典中。使用Twisted中的adbapi获取数据库链接池对象,使用前须要导入adbapi,如:from twisted.enterprise import adbapi
。使用时须要用到ConnectionPool链接池:pool=adbapi.ConnectionPool('MySQLdb',**params)
,参数MySQLdb是使用的数据库引擎的名字,params就是要传递的数据库配置信息;api
接着在process_item()方法中使用数据库链接池对象进行数据库操做,自动传递cursor对象到数据库操做方法runInteraction()的第一个参数(自定义方法)如:ret=self.connection_pool.runInteraction(self.mysql_insert,item)
;服务器
还能够设置出错时的回调方法,自动传递出错消息对象failure到错误处理方法的第一个参数(自定义方法)如:ret.addErrback(self.error_callback)
;网络
最后记得修改settings文件中的ITEM_PIPELINES配置,如:'XKD_Dribbble_Spider.pipelines.AIOMysqlItemPipeline': 2
;框架
from twisted.enterprise import adbapi import MySQLdb.cursors class AIOMysqlItemPipeline: def __init__(self, pool): self.connection_pool = pool # 1:调用类方法 @classmethod def from_settings(cls, settings): connkw = { 'host': MYSQL_HOST, 'user': MYSQL_USER, 'password': MYSQL_PASSWORD, 'db': MYSQL_DATABASE, 'port': MYSQL_PORT, 'use_unicode': MYSQL_OPTIONAL.get('USE_UNICODE'), 'charset': MYSQL_OPTIONAL.get('CHARSET'), 'cursorclass': MySQLdb.cursors.DictCursor, } pool = adbapi.ConnectionPool('MySQLdb', **connkw) return cls(pool) # 2:执行process_item def process_item(self, item, spider): ret = self.connection_pool.runInteraction(self.mysql_insert, item) ret.addErrback(self.error_callback) def mysql_insert(self, cursor, item): sql = 'insert into item(title, image_url, date, image_path, url, url_id)' \ 'values (%s, %s, %s, %s, %s, %s)' date = item['date'] cursor.execute(sql, args=(item['title'], item['image_url'], date, item['image_path'], item['url'], item['url_id'])) def error_callback(self, error): print('insert_error =========== {}'.format(error)) 修改settings文件 ITEM_PIPELINES = { # 'XKD_Dribbble_Spider.pipelines.XkdDribbbleSpiderPipeline': 300, # 当items.py模块yield以后,默认就是下载image_url的页面 'XKD_Dribbble_Spider.pipelines.ImagePipeline': 1, 'XKD_Dribbble_Spider.pipelines.AIOMysqlItemPipeline': 2, }