Python批量抓取公众号音视频:轻松下载你想要的内容

之前发过Python批量抓取公众号的教程,这次不抓取公众号阅读数数据,批量下载公众号文章,音频和视频,直接上代码:

def video(res, headers,date):
vid = re.search(r'wxv_.{19}',res.text)
# time.sleep(2)
if vid:
vid = vid.group(0)
print('视频id',vid)
url = f'https://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&vid={vid}'
data = requests.get(url,headers=headers,timeout=1).json()
video_url = data['url_info'][0]['url']
video_data = requests.get(video_url,headers=headers)
print('正在下载视频:'+trimName(data['title'])+'.mp4')
with open(date+'___'+trimName(data['title'])+'.mp4','wb') as f:
f.write(video_data.content)
def audio(res,headers,date,title):
aids = re.findall(r'"voice_id":"(.*?)"',res.text)
time.sleep(2)
tmp = 0
for id in aids:
tmp +=1
url = f'https://res.wx.qq.com/voice/getvoice?mediaid={id}'
audio_data = requests.get(url,headers=headers)
print('正在下载音频:'+title+'.mp3')
with open(date+'___'+trimName(title)+'___'+str(tmp)+'.mp3','wb') as f5:
f5.write(audio_data.content)
url = input('请输入文章链接:')
response = requests.get(url, headers=headers)
urls = re.findall('<a target="_blank" href="(https?://mp.weixin.qq.com/s?.*?)"',response.text)
urls.append(url)
print('文章总数',len(urls))
for mp_url in urls:
res = requests.get(html.unescape(mp_url),proxies={'http': None,'https': None},verify=False, headers=headers)
content = res.text.replace('data-src', 'src').replace('//res.wx.qq.com', 'https://res.wx.qq.com')
try:
title = re.search(r'var msg_title = '(.*)'', content).group(1)
ct = re.search(r'var ct = "(.*)";', content).group(1)
date = time.strftime('%Y-%m-%d', time.localtime(int(ct)))
print(date,title)
audio(res,headers,date,title)
video(res,headers,date)
with open(date+'_'+title+'.html', 'w', encoding='utf-8') as f:
f.write(content)
except Exception as err:
with open(str(randint(1,10))+'.html', 'w', encoding='utf-8') as f:
f.write(content)
下载的音频,视频在当前目录,文章html可以用python再转pdf。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执刀人的工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值