欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Scrapy --异步插入数据库

程序员文章站 2022-03-03 07:52:47
...
  • 在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)