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.
-
Does this answer your question? Download file from web in Python 3ggorlen– ggorlen2021-02-01 14:34:22 +00:00Commented Feb 1, 2021 at 14:34
Add a comment
|
2 Answers
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())
3 Comments
icktoofay
You might consider using
"wb" rather than "w" in case the downloaded file is binary.user1044824
Thanks it working very well, now I just have to understand why exactly. =)
chown
@user which part doesnt make sense? Ill try to explain more if possible.