import os from bs4 import BeautifulSoup import re 指定文件夹路径 folder_path = "C:/Users/test/Desktop/DIDItest" 正则表达式模式 pattern = r'<body>(.*?)</body>' 遍历文件夹中的所有文件 for root, dirs, files in os.walk(folder_path): for file in files: # 读取html文件 file_path = os.path.join(root, file) with open(file_path, "r", encoding="utf-8-sig") as f: html_code = f.read() # 创建BeautifulSoup对象 soup = BeautifulSoup(html_code, 'html.parser') # 使用正则表达式匹配<body>标签内的数据 body_data = re.findall(pattern, html_code, re.DOTALL) # 剔除<p>和()</p> body_data = body_data[0].replace("<p>", "").replace("()</p>", "") # 使用正则表达式提取talk_id、时间、发送者ID和接收者ID matches = re.findall(r'\[talkid:(\d+)\](\d+年\d+月\d+日 \d+:\d+:\d+).*?<span.*?>(\d+)<.*?>(.*?)<.*?''((中发言|发送)\s(.*?)\s)', body_data) # 提取唯一ID,时间,发送号码和私聊群聊关键词 matches1 = re.findall(r'<span.*?hint-success.*?>(\d+)', body_data) matches2 = re.findall(r'(?:中发言|发送)\s*(.*?)\s*(?:音频 :|图片 :)?(?:\[([^\]]+)\])?', body_data) # 处理匹配结果 for match in matches: talk_id = match[0] time = match[1] send_id = match[2] talk_type = match[3] content = match[4] # 提取第二个号码为接收号码 if len(matches1) >= 2: receive_id = matches1[3] # 替换字符 time = time.replace('年', '-').replace('月', '-').replace('日', '') talk_type = talk_type.replace('向', '私聊').replace('在群', '群聊') content = content.replace('音频', '').replace('图片', '').replace('发送','').replace('中发言','') content = re.sub(r'\n', '', content) print("---导入完成-----") 使用python 创建sql数据库并将数据导入到sql文件中
时间: 2024-04-17 16:26:39 浏览: 16
抱歉,我之前的回答有误。以下是正确的代码示例来创建一个SQLite数据库并将数据导入到该数据库中:
```python
import os
import sqlite3
from bs4 import BeautifulSoup
import re
# 指定文件夹路径
folder_path = "C:/Users/test/Desktop/DIDItest"
# 正则表达式模式
pattern = r'<body>(.*?)</body>'
# 连接数据库
conn = sqlite3.connect('chat_data.db')
cursor = conn.cursor()
# 创建表格
cursor.execute('''CREATE TABLE IF NOT EXISTS chat_data
(talk_id INT PRIMARY KEY,
time TEXT,
send_id INT,
receive_id INT,
talk_type TEXT,
content TEXT)''')
# 遍历文件夹中的所有文件
for root, dirs, files in os.walk(folder_path):
for file in files:
# 读取html文件
file_path = os.path.join(root, file)
with open(file_path, "r", encoding="utf-8-sig") as f:
html_code = f.read()
# 创建BeautifulSoup对象
soup = BeautifulSoup(html_code, 'html.parser')
# 使用正则表达式匹配<body>标签内的数据
body_data = re.findall(pattern, html_code, re.DOTALL)
# 剔除<p>和()</p>
body_data = body_data[0].replace("<p>", "").replace("()</p>", "")
# 使用正则表达式提取talk_id、时间、发送者ID和接收者ID
matches = re.findall(r'\[talkid:(\d+)\](\d+年\d+月\d+日 \d+:\d+:\d+).*?<span.*?>(\d+)<.*?>(.*?)<.*?''((中发言|发送)\s(.*?)\s)', body_data) # 提取唯一ID,时间,发送号码和私聊群聊关键词
matches1 = re.findall(r'<span.*?hint-success.*?>(\d+)', body_data)
matches2 = re.findall(r'(?:中发言|发送)\s*(.*?)\s*(?:音频 :|图片 :)?(?:\[([^\]]+)\])?', body_data)
# 处理匹配结果
for match in matches:
talk_id = match[0]
time = match[1]
send_id = match[2]
talk_type = match[3]
content = match[4]
# 提取第二个号码为接收号码
if len(matches1) >= 2:
receive_id = matches1[3]
else:
receive_id = None
# 替换字符
time = time.replace('年', '-').replace('月', '-').replace('日', '')
talk_type = talk_type.replace('向', '私聊').replace('在群', '群聊')
content = content.replace('音频', '').replace('图片', '').replace('发送','').replace('中发言','')
content = re.sub(r'\n', '', content)
# 插入数据到表格中
cursor.execute("INSERT INTO chat_data VALUES (?, ?, ?, ?, ?, ?)", (talk_id, time, send_id, receive_id, talk_type, content))
# 提交事务并关闭连接
conn.commit()
conn.close()
print("---导入完成并保存到数据库中---")
```
请注意,这段代码将创建一个名为`chat_data.db`的SQLite数据库文件,并在其中创建一个名为`chat_data`的表格,并将数据导入到该表格中。