Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Define your item pipelines here
- #
- # Don't forget to add your pipeline to the ITEM_PIPELINES setting
- # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
- import MySQLdb
- from MySQLdb import Error as MysqlError
- import logging
- db_host = '127.0.0.1'
- db_user = 'root'
- db_password = 'root'
- db_name = 'amazondb'
- db_charset = 'utf8mb4'
- logger = logging.getLogger(__name__)
- class MySQLPipeline(object):
- def process_item(self, item, spider):
- conn = MySQLdb.connect(db_host, db_user, db_password, db_name, charset="utf8mb4", use_unicode=True)
- cursor = conn.cursor()
- table_name = 'product_tracker'
- try:
- placeholder = ", ".join(["%s"] * len(item))
- statement = "INSERT INTO {table} ({columns}) VALUES ({values})".format(table=table_name,
- columns=",".join(item.keys()),
- values=placeholder)
- cursor.execute(statement, list(item.values()))
- conn.commit()
- except MysqlError as e:
- logger.error("Error %d: %s" % (e.args[0], e.args[1]))
- conn.close()
- return item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement