Python 爬虫数据存储:将实验结果文件导入 MySQL 数据库
Python 爬虫数据存储:将实验结果文件导入 MySQL 数据库
本程序提供将实验结果文件(txt、csv、xlsx)导入 MySQL 数据库的功能。程序包含三个函数分别处理不同文件格式,并提供主程序实现用户交互。
流程图
开始
输入文件路径和数据库信息
选择文件类型(txt、csv、xlsx)
根据文件类型调用相应的函数
结束
函数接口设计
-
txt_to_mysql 函数
- 函数名:
txt_to_mysql - 作用: 将 txt 文件中的数据存储到 MySQL 数据库中
- 输入: 文件路径、数据库信息
- 输出: 无
- 函数名:
-
csv_to_mysql 函数
- 函数名:
csv_to_mysql - 作用: 将 csv 文件中的数据存储到 MySQL 数据库中
- 输入: 文件路径、数据库信息
- 输出: 无
- 函数名:
-
excel_to_mysql 函数
- 函数名:
excel_to_mysql - 作用: 将 xlsx 文件中的数据存储到 MySQL 数据库中
- 输入: 文件路径、数据库信息
- 输出: 无
- 函数名:
函数编码和实现
import pymysql
import pandas as pd
def txt_to_mysql(file_path, db_info):
# 读取 txt 文件内容
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 连接 MySQL 数据库
conn = pymysql.connect(host=db_info['host'], port=db_info['port'],
user=db_info['user'], password=db_info['password'],
db=db_info['db'], charset='utf8')
cursor = conn.cursor()
# 创建数据表
cursor.execute("CREATE TABLE IF NOT EXISTS suesnews (id INT AUTO_INCREMENT PRIMARY KEY, content TEXT)")
# 插入数据
cursor.execute("INSERT INTO suesnews (content) VALUES (%s)", (content,))
# 提交事务并关闭连接
conn.commit()
cursor.close()
conn.close()
def csv_to_mysql(file_path, db_info):
# 读取 csv 文件内容
data = pd.read_csv(file_path)
# 连接 MySQL 数据库
conn = pymysql.connect(host=db_info['host'], port=db_info['port'],
user=db_info['user'], password=db_info['password'],
db=db_info['db'], charset='utf8')
cursor = conn.cursor()
# 创建数据表
cursor.execute("CREATE TABLE IF NOT EXISTS dangdang (id INT AUTO_INCREMENT PRIMARY KEY, book_name VARCHAR(255), author VARCHAR(255), price FLOAT)")
# 插入数据
for index, row in data.iterrows():
cursor.execute("INSERT INTO dangdang (book_name, author, price) VALUES (%s, %s, %s)",
(row['book_name'], row['author'], row['price']))
# 提交事务并关闭连接
conn.commit()
cursor.close()
conn.close()
def excel_to_mysql(file_path, db_info):
# 读取 xlsx 文件内容
data = pd.read_excel(file_path)
# 连接 MySQL 数据库
conn = pymysql.connect(host=db_info['host'], port=db_info['port'],
user=db_info['user'], password=db_info['password'],
db=db_info['db'], charset='utf8')
cursor = conn.cursor()
# 创建数据表
cursor.execute("CREATE TABLE IF NOT EXISTS douban (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), score FLOAT, comment TEXT)")
# 插入数据
for index, row in data.iterrows():
cursor.execute("INSERT INTO douban (title, score, comment) VALUES (%s, %s, %s)",
(row['title'], row['score'], row['comment']))
# 提交事务并关闭连接
conn.commit()
cursor.close()
conn.close()
# 主程序
if __name__ == '__main__':
file_path = input("请输入文件路径:")
db_info = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': '123456',
'db': 'test'
}
file_type = input("请输入文件类型(txt、csv、xlsx):")
if file_type == 'txt':
txt_to_mysql(file_path, db_info)
elif file_type == 'csv':
csv_to_mysql(file_path, db_info)
elif file_type == 'xlsx':
excel_to_mysql(file_path, db_info)
else:
print("不支持的文件类型")
完整程序功能
完整的程序实现了将 txt 文件、csv 文件、xlsx 文件中的数据存储到 MySQL 数据库中的功能。用户需要输入文件路径和数据库信息,以及选择文件类型,程序将根据选择的文件类型调用相应的函数进行存储。
注意:
- 请将数据库信息修改为您的实际配置。
- 程序中创建的表名和字段名可以根据您的实际需求进行调整。
- 程序假设文件编码为 UTF-8。如果您使用其他编码,请在读取文件时指定正确的编码格式。
- 程序假设 csv 文件中的数据包含列名,并且列名与数据库表中的字段名一致。
- 程序假设 xlsx 文件中的数据包含列名,并且列名与数据库表中的字段名一致。
希望本程序能够帮助您将爬虫数据存储到 MySQL 数据库中。如果您有任何问题或建议,请随时提出。
原文地址: https://www.cveoy.top/t/topic/puIU 著作权归作者所有。请勿转载和采集!