import os import csv from bs4 import BeautifulSoup # 文件夹路径 folder_path = 'C:\Users\test\Desktop\DIDItest' # CSV文件路径 csv_file = r'C:\Users\test\Desktop\output.csv' # 创建CSV文件并写入表头 with open(csv_file, 'w', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow(['ID', '时间', '发送号码', '接收号码', '发送内容']) # 遍历文件夹内的所有文件 for file_name in os.listdir(folder_path): file_path = os.path.join(folder_path, file_name) # 判断文件是否为HTML文件 if file_name.endswith('.html'): # 打开HTML文件并解析源代码 with open(file_path, 'r', encoding='utf-8') as file: soup = BeautifulSoup(file, 'html.parser') # 提取ID、时间、发送号码、接收号码和发送内容 talk_id = soup.find('span', class_='hint-success').text.strip() time = soup.find('body').contents[0].strip() send_number = soup.find_all('span', class_='hint-success')[0].text.strip() receive_number = soup.find_all('span', class_='hint-success')[1].text.strip() content = soup.find('p').contents[0].strip() # 判断发送内容是否为音频 if '音频' in content: audio_link = soup.find('a')['href'] content = f'音频文件位置:{audio_link}' # 将提取的信息写入CSV文件 with open(csv_file, 'a', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow([talk_id, time, send_number, receive_number, content]) print("数据已成功写入CSV文件。")
时间: 2024-01-29 10:02:54 浏览: 28
代码看起来没有问题,但是有几个地方需要注意:
1. 在Windows中,文件路径使用反斜杠`\`作为路径分隔符。请确保将`folder_path`和`csv_file`中的路径分隔符改为`\\`,或者使用原始字符串(在字符串前面加上`r`)。
2. 请确保文件夹路径和CSV文件路径是正确的,并且有读取和写入的权限。
如果你仍然遇到乱码问题,请提供更多关于乱码的详细信息,例如乱码的具体样式或错误信息,以便我能更好地帮助你解决问题。
相关问题
import os import re import csv from bs4 import BeautifulSoup folder_path = 'C:/Users/test/Desktop/DIDItest' html_files = [] # 遍历文件夹及其子文件夹下所有HTML文件 for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith('.html'): html_files.append(os.path.join(root, file))打开并提取所有html文件源代码
您好!要打开并提取所有HTML文件的源代码,可以按照以下步骤进行操作:
1. 导入所需的库:
```python
import os
from bs4 import BeautifulSoup
```
2. 指定文件夹路径和一个空列表来存储HTML文件的路径:
```python
folder_path = 'C:/Users/test/Desktop/DIDItest'
html_files = []
```
3. 使用`os.walk()`函数遍历文件夹及其子文件夹下的所有文件,并筛选出以`.html`结尾的文件:
```python
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.html'):
html_files.append(os.path.join(root, file))
```
4. 循环遍历所有HTML文件,并使用BeautifulSoup库打开并提取源代码:
```python
for file_path in html_files:
with open(file_path, 'r', encoding='utf-8') as file:
html_source_code = file.read()
soup = BeautifulSoup(html_source_code, 'html.parser')
# 在这里可以对源代码进行进一步的处理或提取需要的信息
```
请注意,这里使用了BeautifulSoup库来解析HTML文件,您需要确保已经安装了该库,可以使用`pip install beautifulsoup4`来安装。
希望能对您有所帮助!如有任何疑问,请随时提问。
import os import csv from bs4 import BeautifulSoup # 设置文件夹路径 folder_path = 'C:/Users/test/Desktop/DIDItest' # 遍历文件夹中的所有文件和子文件夹 for root, dirs, files in os.walk(folder_path): for file_name in files: if file_name.endswith('.html'): file_path = os.path.join(root, file_name) # 打开HTML文件并读取源代码 with open(file_path, 'r', encoding='utf-8') as file: html_content = file.read() # 在这里可以对源代码进行进一步的操作和提取 soup = BeautifulSoup(html_content, 'html.parser') messages = soup.find_all('p') for message in messages: talk_id_tag = message.find_previous('a') if talk_id_tag: talk_id = talk_id_tag.text.strip()[1:] time = message.contents[0].strip().split(',')[0][1:] send_phone = message.find('span', class_='hint-success').text.strip() receive_phone = message.find_all('span', class_='hint-success')[1].text.strip() content_tag = message.find('a') if content_tag: content = content_tag['href'] content_type = '音频' else: content = message.text.strip() content_type = '文本' print("talkid:", talk_id) print("时间:", time) print("发送号码:", send_phone) print("接收号码:", receive_phone) print("发送内容:", content) print()
这段代码有一个缩进问题,最后的`print`语句没有在正确的位置,应该在`for`循环内,以便在每次迭代时打印出提取的信息。请将最后的`print`语句缩进到正确的位置。以下是修改后的代码:
```python
import os
import csv
from bs4 import BeautifulSoup
# 设置文件夹路径
folder_path = 'C:/Users/test/Desktop/DIDItest'
# 遍历文件夹中的所有文件和子文件夹
for root, dirs, files in os.walk(folder_path):
for file_name in files:
if file_name.endswith('.html'):
file_path = os.path.join(root, file_name)
# 打开HTML文件并读取源代码
with open(file_path, 'r', encoding='utf-8') as file:
html_content = file.read()
# 在这里可以对源代码进行进一步的操作和提取
soup = BeautifulSoup(html_content, 'html.parser')
messages = soup.find_all('p')
for message in messages:
talk_id_tag = message.find_previous('a')
if talk_id_tag:
talk_id = talk_id_tag.text.strip()[1:]
time = message.contents[0].strip().split(',')[0][1:]
send_phone = message.find('span', class_='hint-success').text.strip()
receive_phone = message.find_all('span', class_='hint-success')[1].text.strip()
content_tag = message.find('a')
if content_tag:
content = content_tag['href']
content_type = '音频'
else:
content = message.text.strip()
content_type = '文本'
print("talkid:", talk_id)
print("时间:", time)
print("发送号码:", send_phone)
print("接收号码:", receive_phone)
print("发送内容:", content)
print()
```
请注意,这段代码还没有将提取到的信息写入CSV文件,你可以根据需要进一步完善代码来实现写入CSV的功能。