Python之py9-录音自动下载

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import re
import pandas as pd
import numpy as np
import requests


def get_name_url_dict(day):
    """获取表格,读取表格信息"""
    df = pd.read_excel('/Users/mac/Downloads/py9录音.xlsx')
    df = df.dropna(axis=0)
    name_list = df['日期']
    url_list = df[day]

    name_url_dict = zip(name_list, url_list)
    for name, url in name_url_dict:
        yield name, url


def save_mp3(day, name, url, re_url_flag=''):
    """保存MP3"""
    response = requests.get(url)
    data = response.text

    mp3_url = re.findall('"playurl":"(.*?)"', data)

    mp3_response = requests.get(mp3_url[0])
    mp3_data = mp3_response.content

    mp3_filename = os.path.join(f'0{day}', f'{name}{re_url_flag}.mp3')
    with open(mp3_filename, 'wb') as fw:
        fw.write(mp3_data)


if __name__ == '__main__':
    count = 0
    day = 520
    for name, url in get_name_url_dict(day):
        try:
            if ',' in url:
                re_url_list = url.split(',')
                for ind, re_url in enumerate(re_url_list):
                    save_mp3(day, name, re_url, ind)
            else:
                save_mp3(day, name, url)
            count += 1
            print(count, url, name)
        except:
            continue
posted @ 2019-05-20 23:15  B站-水论文的程序猿  阅读(359)  评论(0编辑  收藏  举报