1

I tried to download something from the Internet using Python, I am using urllib.retriever from the urllib module but I just can't get it work. I would like to be able to save the downloaded file to a location of my choice. If someone could explain to me how to do it with clear examples, that would be VERY appreciated.

gary
  • 4,189
  • 2
  • 30
  • 58
  • Does this answer your question? [Download file from web in Python 3](https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3) – ggorlen Feb 01 '21 at 14:34

2 Answers2

7

I suggest using urllib2 like so:

source = urllib2.urlopen("http://someUrl.com/somePage.html").read()
open("/path/to/someFile", "wb").write(source)

You could even shorten it to (although, you wouldnt want to shorten it if you plan to enclose each individual call in a try - except):

open("/path/to/someFile", "wb").write(urllib2.urlopen("http://someUrl.com/somePage.html").read())
chown
  • 50,544
  • 16
  • 131
  • 169
1

You can also use the urllib:

source = urllib.request.urlopen(("full_url")).read()

and then use what chown used above:

open("/path/to/someFile", "wb").write(source)
ntk4
  • 1,159
  • 1
  • 12
  • 17