石家庄市城乡和建设局网站,哪些网站做科技专题,网页前端设计的心得体会,外贸网站建设有用吗1.twisted框架介绍Twisted是用Python实现的基于事件驱动的网络引擎框架#xff1b;Twisted支持许多常见的传输及应用层协议#xff0c;包括TCP、UDP、SSL/TLS、HTTP、IMAP、SSH、IRC以及FTP。就像Python一样#xff0c;Twisted也具有“内置池”(batteries-included)的特点。…1.twisted框架介绍Twisted是用Python实现的基于事件驱动的网络引擎框架Twisted支持许多常见的传输及应用层协议包括TCP、UDP、SSL/TLS、HTTP、IMAP、SSH、IRC以及FTP。就像Python一样Twisted也具有“内置池”(batteries-included)的特点。Twisted对于其支持的所有协议都带有客户端和服务器实现同时附带有基于命令行的工具使得配置和部署产品级的Twisted应用变得非常方便。2.MySQL数据库信息保存到settings文件中首先我们需要把MySQL数据库中的配置信息保存到settings文件中如MYSQL_HOST localhost的形式MYSQL_HOST localhostMYSQL_USER xkdMYSQL_PASSWORD 123456MYSQL_DATABASE item_databaseMYSQL_PORT 3306MYSQL_OPTIONAL dict(USE_UNICODE True,CHARSET utf8,)然后从settings文件中将这些信息导入到pipeline.py文件中使用from .settings import MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_OPTIONALclass MysqlPipeline:def __init__(self):self.conn MySQLdb.connect(hostMYSQL_HOST, userMYSQL_USER, passwordMYSQL_PASSWORD, databaseMYSQL_DATABASE, use_unicodeMYSQL_OPTIONAL.get(USE_UNICODE), charsetMYSQL_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 itemdef spider_closed(self, spider):self.cursor.close()self.conn.close()3.创建异步Pipeline写入数据库首先创建一个用于异步写入数据的AIOMysqlItemPipeline类然后在这个类的初始化方法中创建一个pool连接池然后在from_settings()方法中获取settings文件中的数据库配置信息并将配置信息存入一个字典中。使用Twisted中的adbapi获取数据库连接池对象使用前需要导入adbapi如from twisted.enterprise import adbapi。使用时需要用到ConnectionPool连接池pooladbapi.ConnectionPool(MySQLdb,**params)参数MySQLdb是使用的数据库引擎的名字params就是要传递的数据库配置信息接着在process_item()方法中使用数据库连接池对象进行数据库操作自动传递cursor对象到数据库操作方法runInteraction()的第一个参数(自定义方法)如retself.connection_pool.runInteraction(self.mysql_insert,item)还可以设置出错时的回调方法自动传递出错消息对象failure到错误处理方法的第一个参数(自定义方法)如ret.addErrback(self.error_callback)最后记得修改settings文件中的ITEM_PIPELINES配置如XKD_Dribbble_Spider.pipelines.AIOMysqlItemPipeline: 2from twisted.enterprise import adbapiimport MySQLdb.cursorsclass AIOMysqlItemPipeline:def __init__(self, pool):self.connection_pool pool# 1调用类方法classmethoddef 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_itemdef 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,}