网页内源代码模板如下: <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> [talkid:138031373]2014年4月20日 05:55:45 , <span class="hint-success" data-hint"">111222</span> 向 <span class="hint-success" data-hint="">3234221</span> 发送 我们已经是好友了,开始聊天吧! <p> ()</p> [talkid:138031374]2014年4月20日 06:55:45 , <span class="hint-success" data-hint"">111222</span> 向 <span class="hint-success" data-hint="">1359075</span> 发送 我们已经是好友了,开始聊天吧! <p> ()</p> </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下多个文件夹内的html文件源代码,并爬取源代码中的ID、时间、发送号码、接收号码、发送内容,如果发送内容为音频则提取音频所在位置,反之则保留发送内容,并将爬取的内容写入csv中
时间: 2023-08-14 22:58:17 浏览: 51
以下是一个示例的Python爬虫代码,用于打开指定文件夹下的多个HTML文件,并从源代码中提取ID、时间、发送号码、接收号码和发送内容,并将结果写入CSV文件。
```python
import os
import csv
from bs4 import BeautifulSoup
# 设置文件夹路径
folder_path = 'C:/Users/test/Desktop/DIDItest'
# 创建CSV文件并写入表头
csv_file = open('output.csv', 'w', newline='', encoding='utf-8')
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_content = file.read()
# 使用BeautifulSoup解析HTML源代码
soup = BeautifulSoup(html_content, 'html.parser')
# 提取ID、时间、发送号码、接收号码和发送内容
talk_id = soup.find('body').find('span', class_='hint-success').text.strip()
time = talk_id.split(',')[0][1:]
send_phone = soup.find('body').find_all('span', class_='hint-success')[0].text.strip()
receive_phone = soup.find('body').find_all('span', class_='hint-success')[1].text.strip()
content_tag = soup.find('body').find('p')
if content_tag.find('a'):
content = content_tag.find('a')['href']
else:
content = content_tag.text.strip()
# 将提取的数据写入CSV文件
csv_writer.writerow([talk_id, time, send_phone, receive_phone, content])
# 关闭CSV文件
csv_file.close()
```
请注意,此代码使用了`BeautifulSoup`库来解析HTML源代码,因此您需要确保已经安装了该库。可以使用`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中![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)