<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>python爬虫提取talkid、时间、发送号码、接收号码、信息类型(如果发送的是文字就定义类型为文字、如果是图片就定义为图片,如果是音频就定义为音频)、消息内容(如果发送的是文字就直接提取文字、如果是图片或音频就提取音频图片所在链接地址)
时间: 2023-08-14 15:58:17 浏览: 47
你可以使用Python的正则表达式库re来提取所需的信息。下面是一个示例代码,它可以根据给定的HTML文本提取talkid、时间、发送号码、接收号码、信息类型和消息内容:
```python
import re
html = '''
<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>
'''
# 提取talkid、时间、发送号码、接收号码、信息类型和消息内容的正则表达式模式
pattern = r'\[talkid:(\d+)\](.*?)</span> 向 (.*?) 发送 (.*?):\[(.*?)\]'
matches = re.findall(pattern, html)
# 遍历匹配结果并输出
for match in matches:
talkid = match[0]
time = match[1].strip()
sender = match[2].strip()
receiver = match[3].strip()
type = match[4].strip()
content = re.findall(r'<a href="(.*?)">', match[5])[0] if type in ['音频', '图片'] else match[5]
print("talkid:", talkid)
print("时间:", time)
print("发送号码:", sender)
print("接收号码:", receiver)
print("信息类型:", type)
print("消息内容:", content)
print()
```
这段代码将输出:
```
talkid: 138031370
时间: 2014年4月20日 03:55:45
发送号码: 111222
接收号码: 232323
信息类型: 文字
消息内容: 我们已经是好友了,开始聊天吧!
talkid: 138031371
时间: 2014年4月20日 04:45:45
发送号码: 111222
接收号码: 123456
信息类型: 音频
消息内容: files/f/f123fsasfsfsjdfrhf_n.m4a
talkid: 138031372
时间: 2014年4月20日 04:55:45
发送号码: 111222
接收号码: 123456
信息类型: 图片
消息内容: files/f/f123fsasfsfsjdfrhf_n.jpg
```
请注意,上述代码假设HTML文本的格式固定,并且消息内容中的链接地址是以`<a href="...">`标签包裹的。如果HTML文本的格式有变化,你可能需要相应地调整正则表达式模式。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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:137031381]2014年4月20日 03:55:45 , 2323234 在群 20011 中发言 我们已经是dffwerwer天吧! [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[音频 [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[图片 [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已方式方法方式 [talkid:137031385]2014年4月25日 06:55:45 , 2323234 在群 20011 中发言 我而对方是否的天吧! </body> </html> 使用python爬虫提取body每行<a href后的链接地址,并将其匹配到相对应的talkid中
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
bodydata中字符串如下: [talkid:138031378]2014年4月20日 05:55:45 , 111222 向 323456 发送 我们已经是好友了,开始聊天吧! [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:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已经是好友了,开始聊天吧! [talkid:137031385]2014年4月25日 06:55:45 , 2323234 在群 20011 中发言 我们已经是好友了,开始聊天吧! 使用Python爬虫提取“发送”或“中发言”后的文字,如果包含
标签,则设置content默认为空![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页内源代码模板如下: <body> [talkid:138031370]2014年4月20日 03:55:45 , 111222 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031371]2014年4月20日 04:45:45 , 111222 向 123456 发送 音频 :[音频 () [talkid:138031372]2014年4月20日 04:55:45 , 111222 向 123456 发送 图片 :[图片 () [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[音频 () [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[图片 () [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已经是好友了,开始聊天吧! () </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下所有文件夹内的html文件源代码,并提取每一行<body>之间的数据,剔除 ()
![](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:138031361]204年4月20日 03:55:45 , 434343 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031362]204年4月20日 04:45:45 , 434343 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频
() [talkid:138031363]204年4月20日 04:55:45 , 434343 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片 () [talkid:138031364]204年4月20日 05:55:45 , 434343 向 3234221 发送 我们已经是好友了,开始聊天吧! () [talkid:138031365]204年4月20日 06:55:45 , 434343 向 1359075 发送 我们已经是好友了,开始聊天吧! () </body> </html>提取网页内ID、时间、发送号码、接收号码、信息内容,如果是音频则提取文件地址![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)