Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import pymysql
- from .settings import *
- class SaveMysqlPipeline(object):
- def __init__(self):
- # 连接数据库
- self.conn = pymysql.connect(
- host=MYSQL_HOST,
- port=MYSQL_PORT,
- user=MYSQL_USER,
- password=MYSQL_PASSWD,
- database=MYSQL_DBNAME,
- charset='utf8')
- # 建立游标对象
- self.cursor = self.conn.cursor()
- self.conn.commit()
- def process_item(self, item, spider):
- # 查重处理
- self.cursor.execute("select * from xxx where xx = %s",
- item['xx'])
- # 是否有重复数据
- repetition = self.cursor.fetchone()
- # 重复
- if repetition:
- pass
- else:
- sql = "insert into tablename(xx) "values(%s)"
- params = (item['xx'])
- self.cursor.execute(sql, params)
- self.conn.commit()
- return item
- def close_spider(self, spider):
- self.cursor.close()
- self.conn.close()
Add Comment
Please, Sign In to add comment