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.
Asked
Active
Viewed 2,060 times
1

gary
- 4,227
- 3
- 31
- 58

user1044824
- 57
- 1
- 5
-
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 Answers
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
- 51,908
- 16
- 134
- 170
-
1You might consider using `"wb"` rather than `"w"` in case the downloaded file is binary. – icktoofay Nov 14 '11 at 02:25
-
Thanks it working very well, now I just have to understand why exactly. =) – user1044824 Nov 14 '11 at 03:15
-
@user which part doesnt make sense? Ill try to explain more if possible. – chown Nov 14 '11 at 18:50
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,247
- 1
- 13
- 18