Download file from Blob URL with Python
<p>I wish to have my Python script download the <em>Master data (Download, XLSX)</em> Excel file from this <a href="http://www.xetra.com/xetra-en/instruments/etf-exchange-traded-funds/list-of-tradable-etfs" rel="nofollow">Frankfurt stock exchange webpage</a>.</p>

<p>When to retrieve it with <code>urrlib</code> and <code>wget</code>, it turns out that the URL leads to a <em>Blob</em>  and the file downloaded is only 289 bytes and unreadable.</p>

<blockquote>
  <p><a href="http://www.xetra.com/blob/1193366/b2f210876702b8e08e40b8ecb769a02e/data/All-tradable-ETFs-ETCs-and-ETNs.xlsx" rel="nofollow">http://www.xetra.com/blob/1193366/b2f210876702b8e08e40b8ecb769a02e/data/All-tradable-ETFs-ETCs-and-ETNs.xlsx</a></p>
</blockquote>

<p>I'm entirely unfamiliar with Blobs and have these questions:</p>

<ul>
<li><p>Can the file "behind the Blob" be successfully retrieved using Python?</p></li>
<li><p>If so, is it necessary to uncover the "true" URL behind the Blob â if there is such a thing â and how? My concern here is that the link above won't be static but actually change often.</p></li>
</ul>

<p>That 289 byte long thing might be a HTML code for <code>403 forbidden</code> page. This happen because the server is smart and rejects if your code does not specify a user agent.</p>

<h1>Python 3</h1>

<pre><code># python3
import urllib.request as request

url = 'http://www.xetra.com/blob/1193366/b2f210876702b8e08e40b8ecb769a02e/data/All-tradable-ETFs-ETCs-and-ETNs.xlsx'
# fake user agent of Safari
fake_useragent = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25'
r = request.Request(url, headers={'User-Agent': fake_useragent})
f = request.urlopen(r)

# print or write
print(f.read())
</code></pre>

<h1>Python 2</h1>

<pre><code># python2
import urllib2

url = 'http://www.xetra.com/blob/1193366/b2f210876702b8e08e40b8ecb769a02e/data/All-tradable-ETFs-ETCs-and-ETNs.xlsx'
# fake user agent of safari
fake_useragent = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25'

r = urllib2.Request(url, headers={'User-Agent': fake_useragent})
f = urllib2.urlopen(r)

print(f.read())
</code></pre>

python download blob urllib