python教程

Python批量抓取公众号,下载音频和视频

我的站长站 2022-06-23 人阅读

之前发过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。

相关专题
公众号
公众号
2020-11-21 116

公众号专题为您整理了关于微信公众号相关资源,包含微信公众号模板,微信公众号源码,微信公众号软件工具等等,助您快速学习使用公众号....

相关推荐
  • Python爬取
  • Python抓取
  • Python爬取豆瓣电影top250排行榜

    Python爬取豆瓣电影top250排行榜示例代码,用的parsel和re两个模块,代码如下:import requestsimport csvimport reimport parselwith open("豆瓣top250.csv",mode="w",encoding="utf_8_sig",newline=&#39;&#39;) as f: csv_writer = csv.writer(f) ...

    python教程 40 1年前
  • 百度图库python批量爬取下载代码

    # @风清扬(fqy2022)import requestsimport timeimport os# 创建保存文件夹if os.path.isdir(r&#39;./保存&#39;): print(&#39;已存在文件夹!&#39;)else: os.mkdir(&#39;./保存&#39;) print(&#39;已为您创建文件夹!&#39;) class Image(object)...

    python教程 63 1年前
  • Python平台热搜热文爬取代码

    前言分享一段Python爬取各大平台热搜热文信息,支持微博热搜、抖音热搜、百度实时热点、知乎热榜、虎嗅热文、哔哩哔哩全站排行、豆瓣新片,免去一个一个网站的看了,是站长编辑的福音。提示:此代码为Python代码,需要有一点基础才能运行,如果是才能,我的站长站...

    python教程 48 2年前
  • Python批量抓取公众号,下载音频和视频

    之前发过Python批量抓取公众号的教程,这次不抓取公众号阅读数数据,批量下载公众号文章,音频和视频,直接上代码:def video(res, headers,date): vid = re.search(r&#39;wxv_.{19}&#39;,res.text) # time.sleep(2) if vid: vid = vid.group...

    python教程 96 1年前
  • Python爬虫抓取中国天气并发送到微信

    Python爬取中国天气随便点进一个城市的详细天气预报,这里以北京为例。按照惯例开下F12。这种实时更新的界面一般是通过ajax传入json文件实现的,打开network选项卡刷新验证一下。Python天气爬虫截图url = "https://d1.weather.com.cn/sk_2d/101010100.h...

    python教程 129 2年前
  • python爬虫下载抖音用户所有短视频+无水印方法

    这次分享下载抖音用户所有短视频方法,python爬虫批量抓取,无水印下载,希望和大家多多交流互相学习!获取用户链接方法1、首先在抖音上随机挑选一个小姐姐,用户主页右上角点开,获取分享链接python下载抖音视频截图得到类似分享链接:在抖音,记录美好生活! https:...

    python教程 264 2年前
最新更新