mirror of
https://github.com/NaiboWang/EasySpider.git
synced 2025-04-23 04:34:22 +08:00
MySQL Code Optimize
This commit is contained in:
parent
c3773848c3
commit
bf608c4c24
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
2
ExecuteStage/.vscode/launch.json
vendored
2
ExecuteStage/.vscode/launch.json
vendored
@ -12,7 +12,7 @@
|
|||||||
"justMyCode": false,
|
"justMyCode": false,
|
||||||
// "args": ["--ids", "[7]", "--read_type", "remote", "--headless", "0"]
|
// "args": ["--ids", "[7]", "--read_type", "remote", "--headless", "0"]
|
||||||
// "args": ["--ids", "[9]", "--read_type", "remote", "--headless", "0", "--saved_file_name", "YOUTUBE"]
|
// "args": ["--ids", "[9]", "--read_type", "remote", "--headless", "0", "--saved_file_name", "YOUTUBE"]
|
||||||
"args": ["--ids", "[149]", "--headless", "0", "--user_data", "0", "--keyboard", "0"]
|
"args": ["--ids", "[29]", "--headless", "0", "--user_data", "1", "--keyboard", "0"]
|
||||||
// "args": "--ids '[97]' --user_data 1 --server_address http://localhost:8074 --config_folder '/Users/naibo/Documents/EasySpider/ElectronJS/' --headless 0 --read_type remote --config_file_name config.json --saved_file_name"
|
// "args": "--ids '[97]' --user_data 1 --server_address http://localhost:8074 --config_folder '/Users/naibo/Documents/EasySpider/ElectronJS/' --headless 0 --read_type remote --config_file_name config.json --saved_file_name"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -302,8 +302,8 @@ class myMySQL:
|
|||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
self.host = config["host"]
|
self.host = config["host"]
|
||||||
self.port = config["port"]
|
self.port = config["port"]
|
||||||
self.user = config["username"]
|
self.username = config["username"]
|
||||||
self.passwd = config["password"]
|
self.password = config["password"]
|
||||||
self.db = config["database"]
|
self.db = config["database"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("读取配置文件失败,请检查配置文件:"+config_file+"是否存在,或配置信息是否有误。")
|
print("读取配置文件失败,请检查配置文件:"+config_file+"是否存在,或配置信息是否有误。")
|
||||||
@ -315,21 +315,22 @@ class myMySQL:
|
|||||||
def connect(self):
|
def connect(self):
|
||||||
try:
|
try:
|
||||||
self.conn = pymysql.connect(
|
self.conn = pymysql.connect(
|
||||||
host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db)
|
host=self.host, port=self.port, user=self.username, passwd=self.password, db=self.db)
|
||||||
print("成功连接到数据库。")
|
print("成功连接到数据库。")
|
||||||
print("Successfully connected to the database.")
|
print("Successfully connected to the database.")
|
||||||
except:
|
except:
|
||||||
print("连接数据库失败,请检查配置文件是否正确。")
|
print("连接数据库失败,请检查配置文件是否正确。")
|
||||||
print(
|
print(
|
||||||
"Failed to connect to the database, please check if the configuration file is correct.")
|
"Failed to connect to the database, please check if the configuration file is correct.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
def create_table(self, table_name, parameters):
|
def create_table(self, table_name, parameters):
|
||||||
self.table_name = table_name
|
self.table_name = table_name
|
||||||
self.field_sql = "("
|
self.field_sql = "("
|
||||||
cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
# 检查表是否存在
|
# 检查表是否存在
|
||||||
cursor.execute("SHOW TABLES LIKE '%s'" % table_name)
|
self.cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
|
||||||
result = cursor.fetchone()
|
result = self.cursor.fetchone()
|
||||||
|
|
||||||
sql = "CREATE TABLE " + table_name + \
|
sql = "CREATE TABLE " + table_name + \
|
||||||
" (_id INT AUTO_INCREMENT PRIMARY KEY, "
|
" (_id INT AUTO_INCREMENT PRIMARY KEY, "
|
||||||
@ -364,47 +365,52 @@ class myMySQL:
|
|||||||
# 如果表不存在,创建它
|
# 如果表不存在,创建它
|
||||||
if not result:
|
if not result:
|
||||||
# 执行SQL命令
|
# 执行SQL命令
|
||||||
cursor.execute(sql)
|
self.cursor.execute(sql)
|
||||||
else:
|
else:
|
||||||
print("数据表" + table_name + "已存在。")
|
print(f'数据表 {table_name} 已存在')
|
||||||
print("The data table " + table_name + " already exists.")
|
print(f'The data table {table_name} already exists.')
|
||||||
cursor.close()
|
self.cursor.close()
|
||||||
|
|
||||||
def write_to_mysql(self, OUTPUT, record, types):
|
def write_to_mysql(self, OUTPUT, record, types):
|
||||||
# 创建一个游标对象
|
# 创建一个游标对象
|
||||||
cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
|
|
||||||
for line in OUTPUT:
|
for line in OUTPUT:
|
||||||
for i in range(len(line)):
|
for i in range(len(line)):
|
||||||
if types[i] == "int" or types[i] == "bigInt":
|
if types[i] == "int" or types[i] == "bigInt":
|
||||||
try:
|
try:
|
||||||
line[i] = int(line[i])
|
line[i] = int(line[i])
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
line[i] = 0
|
line[i] = 0
|
||||||
elif types[i] == "double":
|
elif types[i] == "double":
|
||||||
try:
|
try:
|
||||||
line[i] = float(line[i])
|
line[i] = float(line[i])
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
line[i] = 0.0
|
line[i] = 0.0
|
||||||
elif types[i] == "datetime":
|
elif types[i] == "datetime":
|
||||||
try:
|
try:
|
||||||
line[i] = datetime.datetime.strptime(
|
line[i] = datetime.datetime.strptime(
|
||||||
line[i], '%Y-%m-%d %H:%M:%S')
|
line[i], '%Y-%m-%d %H:%M:%S')
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
line[i] = datetime.datetime.strptime(
|
line[i] = datetime.datetime.strptime(
|
||||||
"1970-01-01 00:00:00", '%Y-%m-%d %H:%M:%S')
|
"1970-01-01 00:00:00", '%Y-%m-%d %H:%M:%S')
|
||||||
elif types[i] == "date":
|
elif types[i] == "date":
|
||||||
try:
|
try:
|
||||||
line[i] = datetime.datetime.strptime(
|
line[i] = datetime.datetime.strptime(
|
||||||
line[i], '%Y-%m-%d')
|
line[i], '%Y-%m-%d')
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
line[i] = datetime.datetime.strptime(
|
line[i] = datetime.datetime.strptime(
|
||||||
"1970-01-01", '%Y-%m-%d')
|
"1970-01-01", '%Y-%m-%d')
|
||||||
elif types[i] == "time":
|
elif types[i] == "time":
|
||||||
try:
|
try:
|
||||||
line[i] = datetime.datetime.strptime(
|
line[i] = datetime.datetime.strptime(
|
||||||
line[i], '%H:%M:%S')
|
line[i], '%H:%M:%S')
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
line[i] = datetime.datetime.strptime(
|
line[i] = datetime.datetime.strptime(
|
||||||
"00:00:00", '%H:%M:%S')
|
"00:00:00", '%H:%M:%S')
|
||||||
to_write = []
|
to_write = []
|
||||||
@ -412,21 +418,20 @@ class myMySQL:
|
|||||||
if record[i]:
|
if record[i]:
|
||||||
to_write.append(line[i])
|
to_write.append(line[i])
|
||||||
# 构造插入数据的 SQL 语句
|
# 构造插入数据的 SQL 语句
|
||||||
sql = f"INSERT INTO " + self.table_name + \
|
sql = f'INSERT INTO {self.table_name} {self.field_sql} VALUES ('
|
||||||
" "+self.field_sql+" VALUES ("
|
for _ in to_write:
|
||||||
for item in to_write:
|
|
||||||
sql += "%s, "
|
sql += "%s, "
|
||||||
# 移除最后的逗号并添加闭合的括号
|
# 移除最后的逗号并添加闭合的括号
|
||||||
sql = sql.rstrip(', ') + ")"
|
sql = sql.rstrip(', ') + ")"
|
||||||
# 执行 SQL 语句
|
# 执行 SQL 语句
|
||||||
try:
|
try:
|
||||||
cursor.execute(sql, to_write)
|
self.cursor.execute(sql, to_write)
|
||||||
except pymysql.OperationalError as e:
|
except pymysql.OperationalError as e:
|
||||||
print("Error:", e)
|
print("Error:", e)
|
||||||
print("Try to reconnect to the database...")
|
print("Try to reconnect to the database...")
|
||||||
self.connect()
|
self.connect()
|
||||||
cursor = self.conn.cursor() # 重新创建游标对象
|
self.cursor = self.conn.cursor() # 重新创建游标对象
|
||||||
cursor.execute(sql, to_write) # 重新执行SQL语句
|
self.cursor.execute(sql, to_write) # 重新执行SQL语句
|
||||||
# self.write_to_mysql(OUTPUT, record, types)
|
# self.write_to_mysql(OUTPUT, record, types)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error:", e)
|
print("Error:", e)
|
||||||
@ -441,9 +446,16 @@ class myMySQL:
|
|||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
# 关闭游标和连接
|
# 关闭游标和连接
|
||||||
cursor.close()
|
self.cursor.close()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
try:
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
print("成功关闭数据库。")
|
print("成功关闭数据库。")
|
||||||
print("Successfully closed the database.")
|
print("Successfully closed the database.")
|
||||||
|
except:
|
||||||
|
print("关闭数据库失败。")
|
||||||
|
print("Failed to close the database.")
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user