网页内源代码模板如下: <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中
时间: 2023-08-14 18:58:17 浏览: 74
您可以使用Python的BeautifulSoup库来解析HTML源代码,然使用csv库将数据写入CSV文件。以下是一个示例代码,可以帮助您实现这个功能:
```python
from bs4 import BeautifulSoup
import csv
import os
# 设置文件夹路径
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 in files:
if file.endswith('.html'):
file_path = os.path.join(root, file)
# 打开文件并解析HTML源代码
with open(file_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
# 提取数据并写入CSV文件
talkid = soup.find('body').find_all('p')
for talk in talkid:
text = talk.get_text(strip=True)
data = text.split(',')
if len(data) >= 2:
row = [data[0].strip(), data[1].strip()]
hint_success = talk.find_all('span', class_='hint-success')
for hint in hint_success:
row.append(hint.get_text(strip=True))
a_tag = talk.find('a')
if a_tag:
row.append(a_tag['href'])
else:
row.append('')
csv_writer.writerow(row)
# 关闭CSV文件
csv_file.close()
```
请确保您已经安装了BeautifulSoup和csv库。此代码将遍历指定文件夹下的所有`.html`文件,解析源代码并提取所需的数据,然后将其写入名为`output.csv`的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)
![](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则提取文件所在位置,反之则填写默认空值
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)