Get file name before downloading
<p>I'm trying to build a scraper in python and only want to download new episodes of a podcast. The problem is that I don't know what the file names will be until after the file is downloaded. Is there a way to get the file name before downloading?</p>

<p><code>def download(path, fileName):
    if(not os.path.exists(fileName)):
        wget.download(path)</code> </p>

<p>I'm guessing that url to podcast redirects you to another url. Then you can use <code>requests</code> to get the final url</p>

<pre><code>import requests
final_url = requests.head(url_to_podcast, allow_redirects=True).url
</code></pre>

<p>and then get the filename from the final url</p>

<pre><code>filename = final_url.split('/')[-1]
</code></pre>

python beautifulsoup wget