import pymysql


class StoreSpiderPipeline(object):
    def __init__(self):
        # 连接数据库:
        self.coon = pymysql.connect(host='数据库ip', user='root', password='8888888', port=8888, db='88888')
        #创建游标对象
        self.cursor = self.coon.cursor()
        self.cursor.execute("create table IF NOT EXISTS klkl_4s_shop(city char(50), district char(50), company_name char(100), business char(100), phone char(50), adress char(50));")
        self.coon.commit()

    def process_item(self, item, spider):
        sql = 'insert into klkl_4s_shop(city,district,company_name,business,phone,adress) values (%s,%s,%s,%s,%s,%s)'
        try:
            self.coon.ping(reconnect=True)
            self.cursor.execute(sql, (item['city'], item['district'], item['company_name'], item['business'], item['phone'],item['adress']))
            self.coon.commit()
            print('klkl_4s_shop提交成功')
        except:
            self.coon.rollback()
            print('klkl_4s_shop提交失败')
        return item

 

相关文章:

  • 2021-12-03
  • 2021-07-22
  • 2022-02-08
  • 2021-09-19
  • 2021-07-30
  • 2021-04-11
猜你喜欢
  • 2022-12-23
  • 2021-11-05
  • 2021-09-15
相关资源
相似解决方案