美文网首页
Scrapy --异步插入数据库

Scrapy --异步插入数据库

作者: 暴走的金坤酸奶味 | 来源:发表于2019-01-06 17:27 被阅读0次
    • 在settings中激活管道、设置数据库参数
    • 导入twisted的异步插入数据库的模块
    from twisted.enterprise import adbapi
    from chufang.items import ChufangUserItem, ChufangMenuItem, ChufangTagsItem
    import scrapy, pymysql
    
    
    class ChufangPipeline(object):
        def __init__(self, dbpool):
            self.dbpool = dbpool
    
        @classmethod
        def from_crawler(cls, cralwer):
            db_parmars = {
                'host': cralwer.settings['MYSQL_HOST'],
                'user': cralwer.settings['MYSQL_USER'],
                'passwd': cralwer.settings['MYSQL_PWD'],
                'db': cralwer.settings['MYSQL_DB'],
                'port': cralwer.settings['MYSQL_PORT'],
                'charset': cralwer.settings['MYSQL_CHARSET']
            }
            dbpool = adbapi.ConnectionPool('pymysql', **db_parmars)
            return cls(dbpool)
    
        def process_item(self, item, spider):
            query = self.dbpool.runInteraction(
                self.insert_data_to_mysql,
                item
            )
            query.addErrback(
                self.insert_err,
                item
            )
    
            return item
    
        def insert_data_to_mysql(self, cursor, item):
            data_dict = dict(item)
            sql, data = item.get_insert_sql_data(data_dict)
            cursor.execute(sql, data)
    
        def insert_err(self, failure, item):
            print(failure, '失败', item)
    
    

    相关文章

      网友评论

          本文标题:Scrapy --异步插入数据库

          本文链接:https://www.haomeiwen.com/subject/bdgsrqtx.html