下载前获取文件名

时间:2016-09-06 21:07:53

标签: python beautifulsoup wget

我正在尝试在python中构建一个scraper,只想下载一个播客的新剧集。问题是,在下载文件之前,我不知道文件名是什么。有没有办法在下载之前获取文件名?

def download(path, fileName):
    if(not os.path.exists(fileName)):
        wget.download(path)

1 个答案:

答案 0 :(得分:0)

我猜测播客的网址会将您重定向到另一个网址。然后,您可以使用requests获取最终网址

import requests
final_url = requests.head(url_to_podcast, allow_redirects=True).url

然后从最终网址

获取文件名
filename = final_url.split('/')[-1]