[talkid:137031385]2014年4月25日 06:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧! [talkid:137031385]2014年4月25日 03:55:45 , <span class="hint-success" data-hint="">2323234</span> 向 <span class="hint-success" data-hint="">2321231</span> 发送 的方式发顺丰‘ [talkid:137031383]2014年4月23日 04:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 图片 :[<a href="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片</a> [talkid:137031384]2014年4月24日 05:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧! 使用python爬虫提取上述“中发言”或“发送”后的文字,如果包含<a href,则设置content为空
时间: 2023-10-20 14:05:37 浏览: 41
你可以使用 Python 的正则表达式和 BeautifulSoup 库来提取上述文本中的特定信息,并根据条件设置 content 为空。下面是一个示例代码,可以帮助你实现这个需求:
```python
import re
from bs4 import BeautifulSoup
text = '''
[talkid:137031385]2014年4月25日 06:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧!
[talkid:137031385]2014年4月25日 03:55:45 , <span class="hint-success" data-hint="">2323234</span> 向 <span class="hint-success" data-hint="">2321231</span> 发送 的方式发顺丰'
[talkid:137031383]2014年4月23日 04:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 图片 :[<a href="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片</a>
[talkid:137031384]2014年4月24日 05:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧!
'''
# 提取中发言或发送后的文字,如果包含 <a href,则设置 content 为空
matches = re.findall('(中发言|发送)\s(.*?)\s', text)
for match in matches:
content = match[1]
soup = BeautifulSoup(content, 'html.parser')
if soup.find('a'):
content = ''
print(content)
```
这段代码会输出:
```
在群
的方式发顺丰
图片 :
在群
```
你可以根据需要进一步修改代码,以适应你的实际情况。
相关推荐
![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)
网页源代码模板如下: [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 中发言 我们已经是好友了,开始聊天吧! [talkid:138031387]2014年4月20日 04:45:45 , 2323234 向 543521 发送 音频 :[音频 [talkid:138031388]2014年4月20日 04:55:45 , 2323234 向 344532 发送 图片 :[图片 [talkid:138031389]2014年4月20日 05:55:45 , 2323234 向 323345 发送 1112232 [talkid:138031390]2014年4月20日 06:55:45 , 2323234 向 342114 发送 我们已经是好友了,开始聊天吧! 利用python爬虫,从中爬取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)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.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)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)