Python Scrapy 框架连接mysql数据库

class MySQLPipeline(object):
    def __init__(self):
        # 连接数据库
        self.connect = pymysql.connect(
            host='xxx.xxx.x.xxx',  # 数据库地址
            port=3306,  # 数据库端口
            db='mysql',  # 数据库名
            user='root',  # 数据库用户名
            passwd='xxxxxxx',  # 数据库密码
            charset='utf8',  # 编码方式
            use_unicode=True)
        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor()
    def process_item(self, item, spider):
        self.cursor.execute(
            """insert into test(A ,B,C,D,E,F,G,H,I,J,K,L,M) # 使用%s格式化字段值
value (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",         
            (
                item['A'],
                item['B'],
                item['C'],
                item['D'],
                item['E'],
                item['F'],
                item['G'],
                item['H'],
                item['I'],
                item['J'],
                item['K'],
                item['L'],
                item['M],))
        # 提交sql语句
        self.connect.commit()
        self.cursor.close()
        self.connect.close()
        return item  # 必须实现返回    

作者:WangB

你可能感兴趣的:(Python在石油工程中应用,智能钻完井)