How to download a web pdf file from a url in python ?

Published: May 21, 2019

Tags: Python; urlretrieve;

DMCA.com Protection Status

To download a pdf from a given web url using python, a solution is to use the module urllib. Lets try to download the file available from the following url

http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf

using urlretrieve:

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

Note: it is also possible to directly change the name of the file (we can called it new_file.pdf for instance):

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

See also