bodydata中字符串如下: [talkid:138031378]2014年4月20日 05:55:45 , <span class="hint-success" data-hint="">111222</span> 向 <span class="hint-success" data-hint="">323456</span> 发送 我们已经是好友了,开始聊天吧! [talkid:138031379]2014年4月20日 06:55:45 , <span class="hint-success" data-hint="">111222</span> 向 <span class="hint-success" data-hint="">342112</span> 发送 我们已经是好友了,开始聊天吧! [talkid:137031381]2014年4月20日 03:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧! [talkid:137031382]2014年4月22日 04:45:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 音频 :[<a href="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频</a> [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> 中发言 我们已经是好友了,开始聊天吧! [talkid:137031385]2014年4月25日 06: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 13:05:37 浏览: 42
你可以使用以下代码来提取 "发送" 或 "中发" 后的文字,并在有 "</a href>" 标签时将 content 设置为空:
```python
import re
body_data = '''
[talkid:138031378]2014年4月20日 05:55:45 , <span class="hint-success" data-hint="">111222</span> 向 <span class="hint-success" data-hint="">323456</span> 发送 我们已经是好友了,开始聊天吧!
[talkid:138031379]2014年4月20日 06:55:45 , <span class="hint-success" data-hint="">111222</span> 向 <span class="hint-success" data-hint="">342112</span> 发送 我们已经是好友了,开始聊天吧!
[talkid:137031381]2014年4月20日 03:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧!
[talkid:137031382]2014年4月22日 04:45:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 音频 :[<a href="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频</a>
[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> 中发言 我们已经是好友了,开始聊天吧!
[talkid:137031385]2014年4月25日 06:55:45 , <span class="hint-success" data-hint="">2323234</span> 在群 <span class="hint-success" data-hint="">20011</span> 中发言 我们已经是好友了,开始聊天吧!
'''
matches = re.findall('(中发言|发送)\s(.*?)\s', body_data)
for match in matches:
content = match[1]
if '</a href>' in content:
content = ''
print(content)
```
这段代码将提取 "发送" 或 "中发言" 后的文字,并在 content 中包含 "</a href>" 标签时将其设置为空。你可以根据需要将这部分代码整合到你的爬虫代码中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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爬虫,从中爬取“发送”或“中发言”后的字符串(包括链接地址)
![](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=后的文件名称,如果没有则将该行文件名设置为NONE
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](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: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文件源代码,将源代码转换为字符串格式,
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.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)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)