python下载网页中的附件函数

def save_mp3(mp3_path, mp3_name, url):#路径,名称,下载网址
    if not os.path.exists(mp3_path):#创建文件夹
        os.makedirs(mp3_path)

    else:
        try:
            resp = requests.get(url)
            if resp.status_code == 200:
                file_path = mp3_path + os.path.sep + '{mp3_name}'.format(mp3_name=mp3_name + ".pdf")
                #文件名称
                print(file_path)
                if not os.path.exists(file_path):
                    with open(file_path, 'wb') as f:
                        f.write(resp.content)
                    print('Downloaded autio path is %s' % file_path)
                else:
                    print('Already Downloaded', file_path)
        except requests.ConnectionError:
            print('Failed to Save mp3,item %s' % mp3_name)

猜你喜欢

转载自blog.csdn.net/weixin_41225002/article/details/89472739