How to download a pdf or text file from a url using python ?

Published: April 09, 2020

Tags: Python; urlretrieve; urllib;

DMCA.com Protection Status

Example of how to download a file from a url using python using mpython module urllib and urlretrieve function.

Using python 3

Let's take for example the file available on http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf:

>>> import urllib.request
>>> file_url = 'http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf'
>>> file_output_name = 'file_downloaded.pdf'
>>> urllib.request.urlretrieve(file_url, file_output_name)

Using python 2

Another example in python 2

>>> import urllib
>>> urllib.urlretrieve('http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf', "st-intro.pdf")

See also