1. 程式人生 > >用Python下載視訊,儲存至檔案的demo

用Python下載視訊,儲存至檔案的demo

def download_video(url):
    try:
        print('準備下載視訊:'+url)
        response=requests.get(url,headers=headers)
        data=response.content
        if data:
            file_path='{}/{}.{}'.format(os.getcwd(),md5(data).hexdigest(),'mp4')
            print('檔案為:'+file_path)
            if not os.path.exists(file_path):
                with open(file_path,'wb')as f:
                    f.write(data)
                    f.close()
                    print('視訊下載成功:'+url)
    except Exception:
        print('視訊下載失敗')

os.getcwd()    獲取當前的path

md5.hexdigest(data)    資料進行雜湊