I am trying to download a PDF file from a website and save it to disk. My attempts either fail with encoding errors or result in blank PDFs. In [1]: import requests In [2]: url = 'http://www.hrecos.org//images/Data/forweb/HRTVBSH.Metadata.pdf' In [3]: response = requests.get(url) In [4]: with open('/tmp/metadata.pdf', 'wb') as f: ...: f.write(response.text) --------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call last) <ipython-input-4-4be915a4f032> in <module>() 1 with open('/tmp/metadata.pdf', 'wb') as f: ----> 2 f.write(response.text) 3 UnicodeEncodeError: 'ascii' codec can't encode characters in position 11-14: ordinal not in range(128) In [5]: import codecs In [6]: with codecs.open('/tmp/metadata.pdf', 'wb', encoding='utf8') as f: ...: f.write(response.text) ...: I know it is a codec problem of some kind but I can't seem to get it to work. Continue reading...