"字典形式的批量。Python对城市距离自动化爬取【python小型项目】( 三 )。" />

Python对城市距离自动化爬取【python小型项目】( 三 )


继续写(代码长了,怕显示出错)
#定义在sample_route表中插入数据操作def insert_sample_route(self, condition : "insert语句" ,params : "insert语句的值")->"字典形式的批量插入数据结果":try:self.cur.executemany(condition,params)self.conn.commit()result={'code':'0000','message':'执行批量插入操作成功','data':len(params)}logging.basicConfig(stream=open(config.src_path + '/log/syserror.log', encoding="utf-8", mode="a"), level = logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger = logging.getLogger(__name__)logger.info("在样本表sample_route中插入数据{}条,操作:{}!".format(result['data'],result['message']))except psycopg2.Error as e:self.conn.rollback()# 执行回滚操作result={'code':'9999','message':'执行批量插入异常','data':[]}print ("数据库错误|insert_data : %s" % (e.args[0]))logging.basicConfig(stream=open(config.src_path + '/log/syserror.log', encoding="utf-8", mode="a"), level = logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger = logging.getLogger(__name__)logger.exception(e)return result#定义对表的清空def ini_table(self,tablename:"表名")->"清空表数据结果":try:rows_affect = self.cur.execute("select count(*) from {}".format(tablename))test = self.cur.fetchone()# 获取一条结果self.cur.execute("truncate table {}".format(tablename))self.conn.commit()result={'code':'0000','message':'执行清空表操作成功','data':test[0]}logging.basicConfig(stream=open(config.src_path + '/log/syserror.log', encoding="utf-8", mode="a"), level = logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger = logging.getLogger(__name__)logger.info("清空{}表,操作数据{}条,操作:{}!".format(tablename,result['data'],result['message']))except psycopg2.Error as e:self.conn.rollback()# 执行回滚操作result={'code':'9999','message':'执行批量插入异常','data':[]}print ("数据库错误|insert_data : %s" % (e.args[0]))logging.basicConfig(stream=open(config.src_path + '/log/syserror.log', encoding="utf-8", mode="a"), level = logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger = logging.getLogger(__name__)logger.exception(e)return result#查询表的所有数据def select_all(self, tablename:"表名")->"返回list,存放查询的结果":try:rows_affect = self.cur.execute("select * from {}".format(tablename))test = self.cur.fetchall()# self.cur.execute("truncate table {}".format(tablename))self.conn.commit()result={'code':'0000','message':'查询表成功','data':test}logging.basicConfig(stream=open(config.src_path + '/log/syserror.log', encoding="utf-8", mode="a"), level = logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger = logging.getLogger(__name__)logger.info("清空{}表,操作数据{}条,操作:{}!".format(tablename,result['data'],result['message']))except psycopg2.Error as e:self.conn.rollback()# 执行回滚操作result={'code':'9999','message':'执行批量插入异常','data':[]}print ("数据库错误|insert_data : %s" % (e.args[0]))logging.basicConfig(stream=open(config.src_path + '/log/syserror.log', encoding="utf-8", mode="a"), level = logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')logger = logging.getLogger(__name__)logger.exception(e)return result#数据库关闭def __del__(self):self.conn.close()

Python对城市距离自动化爬取【python小型项目】

文章插图
这里提出来想说一下的打印日志文件的操作,: