网页内源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> <link href="http://192.167.1.101/hint.css" rel="stylesheet"></link> </head> <body>[talkid:138031370]2014年4月20日 03:55:45 , <span class="hint-success" data-hint"">111222</span> 向 <span class="hint-success" data-hint="">232323</span> 发送 我们已经是好友了,开始聊天吧! <p> ()</p> [talkid:138031371]2014年4月20日 04:45:45 , <span class="hint-success" data-hint"">111222</span> 向 <span class="hint-success" data-hint="">123456</span> 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频</a> <p> ()</p> [talkid:138031372]2014年4月20日 04:55:45 , <span class="hint-success" data-hint"">111222</span> 向 <span class="hint-success" data-hint="">123456</span> 发送 图片 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片</a> <p> ()</p> </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下多个文件夹内的html文件源代码,并将源代码转换为字符串,爬取源代码字符串中的ID、时间、发送号码、接收号码、信息类型、发送内容,如果发送内容不为文本,则提取文件所在链接地址,并将爬取的内容写入csv中,talkid提取[]中talkid:后的数字、时间精确至年月日时分秒、发送号码提取第一个 data-hint""></span>之间的数字,接收号码提取第二个data-hint""></span>,信息类型就提取</span> 发送与:之间的文字,如果没有:则定义为文字
时间: 2023-08-14 16:58:16 浏览: 67
您可以使用Python的BeautifulSoup库来解析HTML源代码,然后使用正则表达式来提取所需的信息。以下是一个示例代码,可以实现您的要求:
```python
import os
import re
import csv
from bs4 import BeautifulSoup
# 文件夹路径
folder_path = 'C:/Users/test/Desktop/DIDItest'
# 创建CSV文件并写入标题行
csv_file = open('output.csv', 'w', newline='')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['ID', '时间', '发送号码', '接收号码', '信息类型', '发送内容'])
# 遍历文件夹
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 = file.read()
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html, 'html.parser')
# 提取talkid
talkid_match = re.search(r'\[talkid:(\d+)\]', html)
talkid = talkid_match.group(1) if talkid_match else ''
# 提取时间
time_match = re.search(r'\d{4}年\d{1,2}月\d{1,2}日 \d{2}:\d{2}:\d{2}', html)
time = time_match.group() if time_match else ''
# 提取发送号码
sender_match = re.search(r'<span class="hint-success" data-hint="">(\d+)</span>', html)
sender = sender_match.group(1) if sender_match else ''
# 提取接收号码
receiver_match = re.search(r'<span class="hint-success" data-hint="">(\d+)</span>', html[sender_match.end():])
receiver = receiver_match.group(1) if receiver_match else ''
# 提取信息类型
type_match = re.search(r'</span>(.*?):', html)
message_type = type_match.group(1) if type_match else ''
# 提取发送内容
content_match = re.search(r':(.*?)</p>', html)
content = content_match.group(1) if content_match else ''
# 如果发送内容不是文本,提取链接地址
if not content:
link_match = re.search(r'<ahref="(.*?)">', html)
content = link_match.group(1) if link_match else ''
# 写入CSV文件
csv_writer.writerow([talkid, time, sender, receiver, message_type, content])
# 关闭CSV文件
csv_file.close()
```
请注意,以上代码仅提供了一个基本的示例,您可能需要根据实际情况进行修改和优化。此外,还需要安装BeautifulSoup库(可通过`pip install beautifulsoup4`命令进行安装)。
希望能对您有所帮助!如果您有任何其他问题,请随时提问。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页内源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> </head> <body> [talkid:138031379]2014年4月20日 06:55:45 , 111222 向 342112 发送 我们已经是好友了,开始聊天吧! [talkid:137031381]2014年4月20日 03:55:45 , 2323234 在群 20011 中发言 我们已经是好友了,开始聊天吧! [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[音频 [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[图片 [talkid:138031387]2014年4月20日 04:45:45 , 2323234 向 543521 发送 音频 :[音频 </body> </html> 利用python爬虫,提取消息发送内容、文件名称,发送内容提取‘ 发送’后或中发言后的内容,如果有[<a href则设置为默认空值,文件名称则提取如果有[<a href则提取文件所在位置,反之则填写默认空值
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页内源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> </head> <body>[talkid:138031370]2014年4月20日 03:55:45 , 111222 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031371]2014年4月20日 04:45:45 , 111222 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频
() [talkid:138031372]2014年4月20日 04:55:45 , 111222 向 123456 发送 图片 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片 () </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下多个文件夹内的html文件源代码,并爬取源代码中的ID、时间、发送号码、接收号码、发送内容,如果发送内容不为文本,则提取文件所在链接地址,并将爬取的内容写入csv中![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)