使用异步的twisted框架写入数据

发布时间:2020-08-08 20:31:10 作者:Winter
来源:ITPUB博客 阅读:287

1.twisted框架介绍

mysql-settings-"> 2.MySQL数据库信息保存到settings文件中


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()

3.创建异步Pipeline写入数据库


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,
}

参考: https://www.9xkd.com/user/plan-view.html?id=1784587600

推荐阅读:
  1. Nodejs异步流程框架async的方法
  2. python如何通过twisted实现数据库异步插入

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

twisted 使用 写入

上一篇:【Oracle 12c Flex Cluster专题 】— Leaf Node的故障迁移

下一篇:HIVE数据类型及存储格式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》