# Python爬虫脚本简单练习:微信公众号音频批量采集,自动创建目录保存
# 作者:QQ33732186(承接Python数据采集脚本定制)
import os
import time
import requests
from bs4 import BeautifulSoup
########参数定义
# MP3内容页地址
subpage ='https://mp.weixin.qq.com/s/c_cwAc32MM7etO7VfcxauQ'
# 保存目录,末尾带 / 斜杠
basedir ='c:/temp/'
################
########### 采集函数定义
def getaudio(res, nub=0):
global basedir
# 开始向内容页地址发送请求
response=requests.get(res)
# 获取地址源码
html=response.text
soup=BeautifulSoup(html, 'lxml')
pagesubject = soup.find('h1',attrs={u"class":u"rich_media_title"}).string.strip()
pagesubject = pagesubject.replace("|","_")
savedir = basedir + pagesubject.strip()
# 获取所有声音标签
mpvoices = soup.find_all('mpvoice')
# 遍历提取的声音标签
for n,mid in enumerate(mpvoices):
# 防止名称中有.mp3
mp3name = 'save_%s'%(n+1) if len(mid['name'])==0 else mid['name'].replace(".mp3","")
filesavepath = savedir+'/'+mp3name+'.mp3'
# 判断并自动创建目录
direxists = os.path.exists(savedir)
if not direxists:
print('创建目录:%s' % savedir)
os.makedirs(savedir)
fileexists = os.path.exists(filesavepath)
if not fileexists:
print('正在下载:%s.mp3' % mp3name)
req = requests.get('https://res.wx.qq.com/voice/getvoice?mediaid=' + mid['voice_encode_fileid'])
with open(filesavepath,'wb')as f:
f.write(req.content)
time.sleep(1)
else:
print('---跳过文件:%s.mp3' % mp3name)
print("第%d个主题【%s】完成" % (nub, pagesubject))
##############函数定义结束
getaudio(subpage,1)
print('\n下载任务完成-------------')
quit()
![avatar](https://profile-avatar.csdnimg.cn/ddc7c5544e0c4e59be366d044cf96e7c_rekung.jpg!1)
锐昆
- 粉丝: 6
- 资源: 7
最新资源
- C# winform置托盘图标并闪烁演示源码.zip
- 打包和分发Rust工具.pdf
- SQL中的CREATE LOGFILE GROUP 语句.pdf
- C语言-leetcode题解之第172题阶乘后的零.zip
- C语言-leetcode题解之第171题Excel列表序号.zip
- C语言-leetcode题解之第169题多数元素.zip
- ocr-图像识别资源ocr-图像识别资源
- 图像识别:基于Resnet50 + VGG16模型融合的人体细胞癌症分类模型实现-图像识别资源
- C语言-leetcode题解之第168题Excel列表名称.zip
- C语言-leetcode题解之第167题两数之和II-输入有序数组.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论0